Same name and namespace in other branches
  1. 4.7.x modules/comment.module \comment_form()
  2. 6.x modules/comment/comment.module \comment_form()
  3. 7.x modules/comment/comment.module \comment_form()
1 string reference to 'comment_form'
comment_form_box in modules/comment/comment.module

File

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

Code

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', t('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', t('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,
  );
  if (!isset($edit['format'])) {
    $edit['format'] = FILTER_FORMAT_DEFAULT;
  }
  $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 (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 $form;
}