comment_form

  1. drupal
    1. 4.7
    2. 5 comment.module
    3. 6 comment.module
    4. 7 comment.module
Versions
4.7 – 5 comment_form($edit, $title = NULL)
6 comment_form(&$form_state, $edit, $title = NULL)
7 comment_form($form, &$form_state, $comment)

Code

modules/comment.module, line 1212

<?php
function comment_form($edit, $title = NULL) {
  global $user;

  $op = isset($_POST['op']) ? $_POST['op'] : '';

  if ($user->uid) {
    if ($edit['cid'] && user_access('administer comments')) {
      if ($edit['author']) {
        $author = $edit['author'];
      }
      elseif ($edit['name']) {
        $author = $edit['name'];
      }
      else {
        $author = $edit['registered_name'];
      }

      if ($edit['status']) {
        $status = $edit['status'];
      }
      else {
        $status = 0;
      }

      if ($edit['date']) {
        $date = $edit['date'];
      }
      else {
        $date = format_date($edit['timestamp'], 'custom', 'Y-m-d H:i O');
      }

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

      if ($edit['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' => $edit['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' => $edit['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(t('Published'), t('Not published')),
        '#weight' => -1,
      );

    }
    else {
      $form['_author'] = array(
        '#type' => 'item',
        '#title' => t('Your name'),
        '#value' => theme('username', $user),
      );
      $form['author'] = array(
        '#type' => 'value',
        '#value' => $user->name,
      );
    }
  }
  else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MAY_CONTACT) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 60,
      '#size' => 30,
      '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', 'Anonymous'),
    );

    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#maxlength' => 64,
      '#size' => 30,
      '#default_value' => $edit['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' => $edit['homepage'],
    );
  }
  else if (variable_get('comment_anonymous', COMMENT_ANONYMOUS_MAYNOT_CONTACT) == COMMENT_ANONYMOUS_MUST_CONTACT) {
    $form['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#maxlength' => 60,
      '#size' => 30,
      '#default_value' => $edit['name'] ? $edit['name'] : variable_get('anonymous', 'Anonymous'),
      '#required' => TRUE,
    );

    $form['mail'] = array(
      '#type' => 'textfield',
      '#title' => t('E-mail'),
      '#maxlength' => 64,
      '#size' => 30,
      '#default_value' => $edit['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' => $edit['homepage'],
    );
  }

  if (variable_get('comment_subject_field', 1) == 1) {
    $form['subject'] = array(
      '#type' => 'textfield',
      '#title' => t('Subject'),
      '#maxlength' => 64,
      '#default_value' => $edit['subject'],
    );
  }

  $form['comment_filter']['comment'] = array(
    '#type' => 'textarea',
    '#title' => t('Comment'),
    '#rows' => 15,
    '#default_value' => $edit['comment'] ? $edit['comment'] : $user->signature,
    '#required' => TRUE,
  );
  $form['comment_filter']['format'] = filter_form($edit['format']);

  $form['cid'] = array(
    '#type' => 'value',
    '#value' => $edit['cid'],
  );
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => $edit['pid'],
  );
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $edit['nid'],
  );
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $edit['uid'],
  );

  $form['preview'] = array(
    '#type' => 'button',
    '#value' => t('Preview comment'),
    '#weight' => 19,
  );
  $form['#token'] = 'comment' . $edit['nid'] . $edit['pid'];

  // Only show post button if preview is optional or if we are in preview mode.
  // We show the post button in preview mode even if there are form errors so that
  // optional form elements (e.g., captcha) can be updated in preview mode.
  if (!form_get_errors() && ((variable_get('comment_preview', COMMENT_PREVIEW_REQUIRED) == COMMENT_PREVIEW_OPTIONAL) || ($op == t('Preview comment')) || ($op == t('Post comment')))) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Post comment'),
      '#weight' => 20,
    );
  }

  if ($op == t('Preview comment')) {
    $form['#after_build'] = array('comment_form_add_preview');
  }

  if ($_REQUEST['destination']) {
    $form['#attributes']['destination'] = $_REQUEST['destination'];
  }

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

  // Graft in extra form additions
  $form = array_merge($form, comment_invoke_comment($form, 'form'));

  return theme('box', $title, drupal_get_form('comment_form', $form));
}
?>