1 comment.module comment_form_node_form_alter(&$form, $form_state)

Implements hook_form_BASE_FORM_ID_alter().

File

core/modules/comment/comment.module, line 1202
Enables users to comment on published content.

Code

function comment_form_node_form_alter(&$form, $form_state) {
  $node = $form['#node'];
  $form['comment_settings'] = array(
    '#type' => 'fieldset',
    '#access' => user_access('administer comment settings'),
    '#title' => t('Comment settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array('comment-node-settings-form'),
    ),
    '#attached' => array(
      'js' => array(backdrop_get_path('module', 'comment') . '/js/comment.admin.js'),
    ),
    '#weight' => 80,
  );
  $comment_count = isset($node->nid) ? db_query('SELECT comment_count FROM {node_comment_statistics} WHERE nid = :nid', array(':nid' => $node->nid))->fetchField() : 0;
  $comment_settings = ($node->comment == COMMENT_NODE_HIDDEN) ? COMMENT_NODE_CLOSED : $node->comment;
  $form['comment_settings']['comment'] = array(
    '#type' => 'radios',
    '#title' => t('Comments'),
    '#title_display' => 'invisible',
    '#parents' => array('comment'),
    '#default_value' => $comment_settings,
    '#options' => array(
      COMMENT_NODE_OPEN => t('Open'),
      COMMENT_NODE_CLOSED => t('Closed'),
    ),
    COMMENT_NODE_OPEN => array(
      '#description' => t('People with the "Post comments" permission will be able to post comments.'),
    ),
    COMMENT_NODE_CLOSED => array(
      '#description' => t('People will not be able to post comments.'),
    ),
  );
  // If the node has comments, add the "hidden" option too.
  if (!empty($comment_count)) {
    $form['comment_settings']['comment_hidden'] = array(
      '#type' => 'checkbox',
      '#title' => t('Hide existing comments.'),
      '#default_value' => ($node->comment == COMMENT_NODE_HIDDEN) ? TRUE : FALSE,
      '#states' => array(
        'visible' => array(
          ':input[name="comment"]' => array('value' => COMMENT_NODE_CLOSED),
        ),
      ),
    );
  }

  $node_type = node_type_load($node->type);
  $enable = $node_type->settings['comment_close_enabled'];

  if ($enable) {
    $form['comment_settings']['comment_close_enabled'] = array(
      '#type' => 'checkbox',
      '#title' => t('Automatically close comments @close-days days after the Authored on date', array('@close-days' => $node_type->settings['comment_close_days'])),
      '#description' => t('If unchecked, comments will stay on until manually closed.'),
      // Opposite value is saved. We want to have the *unchecked* box mean the
      // "override" is enabled. So we reverse the value to display.
      '#default_value' => isset($node->comment_close_override) ? !$node->comment_close_override : 1,
      '#states' => array(
        'visible' => array(
          ':input[name="comment"]' => array('value' => COMMENT_NODE_OPEN),
        ),
      ),
    );
  }
}