You are here

function discussthis_form_alter in Discuss This! 7

Same name and namespace in other branches
  1. 5 discussthis.module \discussthis_form_alter()
  2. 6 discussthis.module \discussthis_form_alter()
  3. 7.2 discussthis.module \discussthis_form_alter()

Implements hook_form_alter().

Adds per-node override forum dropdown and topic autocomplete form for node edit forms.

File

./discussthis.module, line 130
Associations discussions in forums with specific nodes

Code

function discussthis_form_alter(&$form, &$form_state, $form_id) {

  //
  if ($form_id == 'forum_node_form' && !empty($form_state['discussthis']['post'])) {

    // we need to tweak forum forms to make sure that captcha do not
    // both us when we're auto-posting a new entry in the forum
    // if the user has a captcha on the forum, we need to add a dummy entry
    if (module_exists('captcha') && (arg(0) != 'admin' || variable_get('captcha_allow_on_admin_pages', FALSE)) && !user_access('skip CAPTCHA')) {
      module_load_include('inc', 'captcha');
      $captcha_point = captcha_get_form_id_setting('forum_node_form');
      if ($captcha_point && $captcha_point->type) {

        // the captcha type is set to none so it will be ignored
        $form['buttons']['captcha']['#captcha_type'] = 'none';
      }
    }
  }

  // If the user has no rights to override the Discuss This! settings: return.
  if (!user_access('override discuss this forums')) {
    return;
  }

  // Retrieve the content type of the current modified form.
  if (isset($form['type']['#value'])) {
    $type = $form['type']['#value'];
  }
  elseif (isset($form['orig_type']['#value'])) {
    $type = $form['orig_type']['#value'];
  }
  else {
    return;
  }

  // We only support node forms => return if it's not a node.
  if ($form_id != $type . '_node_form') {
    return;
  }

  // We know it is a node, attempt loading info about it
  // WARNING: $nid can be NULL or 0 when creating a new node.
  $nid = $form['nid']['#value'];

  // Only modify the form if this node type is discussthis-enabled.
  // Note that a type may be turned off, but if the node already
  // had a discussthis post attached to it, then it is still editable
  $forum_tid = _discussthis_get_forum($nid, $type);
  if (!$forum_tid) {
    return;
  }
  $form['discussthis'] = array(
    '#type' => 'fieldset',
    '#title' => t('Discuss This'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
    '#group' => 'additional_settings',
  );
  $name = '';

  // Retrieve the topic nid attached with this node.
  // NOTE: the topic is 0 if no comments as yet been done.
  $topic_nid = _discussthis_get_topic($nid);
  if ($topic_nid) {

    // there is a topic linked, is the node still available?
    $topic = node_load($topic_nid);
    if ($topic) {

      // TODO: also get the name of the forum to display to the user
      $name = '<strong>' . t('Current topic #@nid: @title', array(
        '@title' => $topic->title,
        '@nid' => $topic_nid,
      )) . '</strong><br /><br />';
    }
    else {
      $sql = "DELETE FROM {discussthis} WHERE nid = %d";

      // TODO Please review the conversion of this statement to the D7 database API syntax.

      /* db_query($sql, $nid) */
      db_delete('discussthis')
        ->condition('nid', $nid)
        ->execute();
      unset($topic_nid);
    }
  }

  // This case is when no topic exists for the current node: ie no yet comment.
  if (!$topic_nid) {

    // Let users override the default module of new discussion
    // (this is not shown unless there is a discussion).
    // Retrieve the , or "No Discuss This!"
    $discussthis_forums = _discussthis_build_forums_selection_list();
    $discussthis_forums[0] = 'No Discuss This!';
    $form['discussthis']['discussthis_forum'] = array(
      '#type' => 'select',
      '#title' => t('Forum for new discussion'),
      '#required' => TRUE,
      '#description' => t('Select the forum where the first discussion about this node will be created. Or select "No Discuss This!" to prevent discussions on this node. Note: if a topic is attached to this node, then this parameter will have no effect.'),
      '#options' => $discussthis_forums,
      '#default_value' => $forum_tid,
    );
  }

  // let the user assign a forum topic to this node
  // this can be done at the time a node is being created
  // any forum is acceptable (although we could have flags to know whether that's the case...)
  $form['discussthis']['discussthis_topic'] = array(
    '#type' => 'textfield',
    '#title' => t('Identifier of the forum topic to attach to this node'),
    '#description' => $name . t('Override default topic associated with this node. Leave empty for default topic. Enter a topic name and use autocompletion to associate this node with an existing topic.'),
    '#default_value' => $topic_nid ? $topic_nid : '',
    '#autocomplete_path' => 'discussthis/autocomplete',
    '#maxlength' => 255,
  );
}