comment_form

Versions
4.7 – 5
comment_form($edit, $title = NULL)
6
comment_form(&$form_state, $edit, $title = NULL)
7
comment_form($form, &$form_state, $comment)

Generate the basic commenting form, for appending to a node or display on a separate page.

See also

comment_form_validate()

@see comment_form_submit()

Related topics

Code

modules/comment/comment.module, line 1643

<?php
function comment_form($form, &$form_state, $comment) {
  global $user;

  $op = isset($_POST['op']) ? $_POST['op'] : '';
  $node = node_load($comment->nid);
  $form['#node'] = $node;

  if (!$user->uid && variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
    $form['#attached']['library'][] = array('system', 'cookie');
    $form['#attributes']['class'][] = 'user-info-from-cookie';
  }

  $comment = (array) $comment;
  // Take into account multi-step rebuilding.
  if (isset($form_state['comment'])) {
    $comment = $form_state['comment'] + (array) $comment;
  }
  $comment += array('name' => '', 'mail' => '', 'homepage' => '');
  $comment = (object) $comment;

  if (isset($form_state['comment_preview'])) {
    $form += $form_state['comment_preview'];
  }

  if ($user->uid) {
    if (!empty($comment->cid) && user_access('administer comments')) {
      if (!empty($comment->author)) {
        $author = $comment->author;
      }
      elseif (!empty($comment->name)) {
        $author = $comment->name;
      }
      else {
        $author = $comment->registered_name;
      }

      if (isset($comment->status)) {
        $status = $comment->status;
      }
      else {
        $status = COMMENT_NOT_PUBLISHED;
      }

      if (!empty($comment->date)) {
        $date = $comment->date;
      }
      else {
        $date = format_date($comment->changed, 'custom', 'Y-m-d H:i O');
      }

      $form['admin'] = array(
        '#type' => 'fieldset',
        '#title' => t('Administration'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#weight' => -2,
      );

      if ($comment->registered_name != '') {
        // The comment is by a registered user.
        $form['admin']['author'] = array(
          '#type' => 'textfield',
          '#title' => t('Authored by'),
          '#size' => 30,
          '#maxlength' => 60,
          '#autocomplete_path' => 'user/autocomplete',
          '#default_value' => $author,
          '#weight' => -1,
        );
      }
      else {
        // The comment is by an anonymous user.
        $form['is_anonymous'] = array(
          '#type' => 'value',
          '#value' => TRUE,
        );
        $form['admin']['name'] = array(
          '#type' => 'textfield',
          '#title' => t('Authored by'),
          '#size' => 30,
          '#maxlength' => 60,
          '#default_value' => $author,
          '#weight' => -1,
        );
        $form['admin']['mail'] = array(
          '#type' => 'textfield',
          '#title' => t('E-mail'),
          '#maxlength' => 64,
          '#size' => 30,
          '#default_value' => $comment->mail,
          '#description' => t('The content of this field is kept private and will not be shown publicly.'),
        );
        $form['admin']['homepage'] = array(
          '#type' => 'textfield',
          '#title' => t('Homepage'),
          '#maxlength' => 255,
          '#size' => 30,
          '#default_value' => $comment->homepage,
        );
      }
      $form['admin']['date'] = array(
        '#type' => 'textfield',
        '#parents' => array('date'),
        '#title' => t('Authored on'),
        '#size' => 20,
        '#maxlength' => 25,
        '#default_value' => $date,
        '#weight' => -1,
      );
      $form['admin']['status'] = array(
        '#type' => 'radios',
        '#parents' => array('status'),
        '#title' => t('Status'),
        '#default_value' => $status,
        '#options' => array(COMMENT_NOT_PUBLISHED => t('Not published'), COMMENT_PUBLISHED => t('Published')),
        '#weight' => -1,
      );
    }
    else {
      $form['_author'] = array(
        '#type' => 'item',
        '#title' => t('Your name'),
        '#markup' => theme('username', array('account' => $user)),
      );
      $form['author'] = array(
        '#type' => 'value',
        '#value' => $user->name,
      );
    }
  }
  elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 60,
      '#size' => 30,
      '#default_value' => $comment->name ? $comment->name : variable_get('anonymous', t('Anonymous')),
    );
    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#maxlength' => 64,
      '#size' => 30,
      '#default_value' => $comment->mail, '#description' => t('The content of this field is kept private and will not be shown publicly.'),
    );
    $form['homepage'] = array(
      '#type' => 'textfield',
      '#title' => t('Homepage'),
      '#maxlength' => 255,
      '#size' => 30,
      '#default_value' => $comment->homepage,
    );
  }
  elseif (variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 60,
      '#size' => 30,
      '#default_value' => $comment->name ? $comment->name : variable_get('anonymous', t('Anonymous')),
      '#required' => TRUE,
    );
    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#maxlength' => 64,
      '#size' => 30,
      '#default_value' => $comment->mail, '#description' => t('The content of this field is kept private and will not be shown publicly.'),
      '#required' => TRUE,
    );
    $form['homepage'] = array(
      '#type' => 'textfield',
      '#title' => t('Homepage'),
      '#maxlength' => 255,
      '#size' => 30,
      '#default_value' => $comment->homepage,
    );
  }

  if (variable_get('comment_subject_field_' . $node->type, 1) == 1) {
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 64,
      '#default_value' => !empty($comment->subject) ? $comment->subject : '',
    );
  }

  if (!empty($comment->comment)) {
    $default = $comment->comment;
  }
  else {
    $default = '';
  }

  $form['comment'] = array(
    '#type' => 'textarea',
    '#title' => t('Comment'),
    '#rows' => 15,
    '#default_value' => $default,
    '#text_format' => isset($comment->format) ? $comment->format : filter_default_format(),
    '#required' => TRUE,
  );

  $form['cid'] = array(
    '#type' => 'value',
    '#value' => !empty($comment->cid) ? $comment->cid : NULL,
  );
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => !empty($comment->pid) ? $comment->pid : NULL,
  );
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $comment->nid,
  );
  $form['language'] = array(
    '#type' => 'value',
    '#value' => isset($comment->language) ? $comment->language : '',
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => !empty($comment->uid) ? $comment->uid : 0,
  );
  $form['node_type'] = array(
    '#type' => 'value',
    '#value' => 'comment_node_' . $node->type,
  );

  // Only show the save button if comment previews are optional or if we are
  // already previewing the submission. However, if there are form errors,
  // we hide the save button no matter what, so that optional form elements
  // (e.g., captchas) can be updated.
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#access' => variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_REQUIRED || (!form_get_errors() && isset($form_state['comment_preview'])),
    '#weight' => 19,
  );
  $form['preview'] = array(
    '#type' => 'submit',
    '#value' => t('Preview'),
    '#access' => (variable_get('comment_preview_' . $node->type, DRUPAL_OPTIONAL) != DRUPAL_DISABLED),
    '#weight' => 20,
    '#submit' => array('comment_form_build_preview'),
  );
  $form['#token'] = 'comment' . $comment->nid . (isset($comment->pid) ? $comment->pid : '');

  if (empty($comment->cid) && empty($comment->pid)) {
    $form['#action'] = url('comment/reply/' . $comment->nid);
  }

  $comment->node_type = 'comment_node_' . $node->type;
  $form['#builder_function'] = 'comment_form_submit_build_comment';
  field_attach_form('comment', $comment, $form, $form_state);

  return $form;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.