You are here

function discussthis_form_alter in Discuss This! 5

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

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

Parameters

form_id the id of the form to be altered:

form the form itself:

File

./discussthis.module, line 223

Code

function discussthis_form_alter($form_id, &$form) {

  // 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'])) {
    $type = $form['type']['#value'];
  }
  elseif (isset($form['orig_type'])) {
    $type = $form['orig_type']['#value'];
  }
  else {
    return;
  }

  # First check if this is the forum topic node form
  if ($type == 'forum' && isset($_GET['did'])) {

    #ob_start(); print_r($form);

    #drupal_set_message("form values: ".ob_get_contents());

    #ob_end_clean();

    # TODO: Check the user actually has access to this node(to avoid coming here directly)
    $discuss_node = node_load($_GET['did']);
    $default_body = 'Following is a discussion on the [node-type-name] item titled: [node-link]' . '.<br /> Below is the discussion so far. Feel free to add your own comments!<br />';
    $form['title']['#type'] = 'item';

    # Make the title read-only
    $form['title']['#value'] = token_replace(variable_get('discussthis_newsubject', 'Discuss This: [node-title]'), 'discussthis', $discuss_node);
    $desc = token_replace(variable_get('discussthis_newtemplate', $default_body), 'discussthis', $discuss_node);
    $form['discussthis'] = array(
      '#type' => 'fieldset',
      '#title' => t('Discuss This'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
      '#weight' => -2,
    );
    $form['discussthis']['description'] = array(
      '#type' => 'item',
      #'#title' => t('Info'),
      '#value' => $desc,
    );
    $form['discuss_nid'] = array(
      '#type' => 'value',
      '#value' => $discuss_node->nid,
    );
  }

  # only modify the form if this node type is discussthis-enabled
  if (!variable_get('discussthis_node_' . $type, 0)) {
    return;
  }

  // The discuss this link is enabled on a per node type basis. The variable used to store
  // this information is named using both the name of this module, to avoid namespace
  // conflicts, and the node type, because we support multiple node types.
  switch ($form_id) {
    case $type . '_node_form':

      // here we need to look up any existing forum topic for this node, for use in the autocomplete field below..
      $nid = $form['nid']['#value'];
      $forum_tid = _discussthis_get_forum($nid, $type);
      $forum_nid = _discussthis_get_topic($nid);
      $topic = node_load(array(
        'nid' => $forum_nid,
      ));

      // If discuss this is enabled for this node type, insert the fieldset
      if ($forum_tid && user_access('override discuss this forums')) {
        $forums = forum_get_forums();
        foreach ($forums as $tid => $forum) {
          if (!$forum->container) {
            $discussthis_forums[$tid] = $forum->name;
          }
        }
        $form['discussthis'] = array(
          '#type' => 'fieldset',
          '#title' => t('Discuss This'),
          '#collapsible' => true,
          '#collapsed' => true,
          '#weight' => 1,
          '#tree' => true,
        );
        $form['discussthis']['forum'] = array(
          '#type' => 'select',
          '#title' => t('Forum for new discussions on this node'),
          '#required' => true,
          '#description' => t('Select the forum where a NEW discussion will be created if none exist.'),
          '#options' => $discussthis_forums,
          '#default_value' => $forum_tid,
          '#weight' => 2,
        );
        $form['discussthis']['topic'] = array(
          '#type' => 'textfield',
          '#title' => t('Forum topic for discussions on this node'),
          '#description' => t('The forum topic where discussions of this node appear'),
          '#default_value' => $topic->title,
          '#autocomplete_path' => 'discussthis/autocomplete/' . $forum_tid,
          '#maxlength' => 255,
          '#weight' => 3,
        );
      }
      break;
  }
}