You are here

function discussthis_form_alter in Discuss This! 6

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

\brief hook_form_alter implementation

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

\param[in] $form the form itself \param[in,out] $form_state the current state of the form \param[in] $form_id the id of the form to be altered

File

./discussthis.module, line 519
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';
      }
    }
  }

  // can the user override the Discuss This! selection?
  if (!user_access('override discuss this forums')) {
    return;
  }

  // we're only modifying node forms, if the type field isn't set we don't need
  // to bother; otherwise, store it for later retrieval.
  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
  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
  $discussthis_forum = _discussthis_get_forum($nid, $type);
  $forum_tid = $discussthis_forum['forum_tid'];
  if (!$forum_tid) {
    return;
  }
  $form['discussthis'] = array(
    '#type' => 'fieldset',
    '#title' => t('Discuss This'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $name = '';
  $topic_nid = _discussthis_get_topic($nid);
  if ($topic_nid) {

    // there is a topic linked, is the node still available?
    $topic = node_load(array(
      'nid' => $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";
      db_query($sql, $nid);
      unset($topic_nid);
    }
  }
  if (!$topic_nid) {

    // let users override the default module of new discussion
    // (this is not shown unless there is a discussion)
    // list of forums, or "No Discuss This!"
    $forums = forum_get_forums();
    $discussthis_forums[-1] = 'No Discuss This!';
    foreach ($forums as $tid => $forum) {
      if (empty($forum->container)) {
        $discussthis_forums[$tid] = $forum->name;
      }
    }
    $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('To use the auto-complete feature, enter the title of a topic to attach to this node. Otherwise, simply enter the topic identifier directly. Clear the identifier or use 0 to detach this node from the current topic it is attached to. New node can immediately be attached to a discussion topic. WARNING: this feature does not prevent you from selecting a topic from a forum that is not otherwise explicitly connected with the Discuss This! module.'),
    '#default_value' => $topic_nid ? $topic_nid : '',
    '#autocomplete_path' => 'discussthis/autocomplete',
    '#maxlength' => 255,
  );
}