public function CommentFormController::form

Overrides Drupal\Core\Entity\EntityFormController::form().

Overrides EntityFormController::form

File

drupal/core/modules/comment/lib/Drupal/comment/CommentFormController.php, line 22
Definition of Drupal\comment\CommentFormController.

Class

CommentFormController
Base for controller for comment forms.

Namespace

Drupal\comment

Code

public function form(array $form, array &$form_state, EntityInterface $comment) {
  global $user;
  $node = node_load($comment->nid);
  $form_state['comment']['node'] = $node;

  // Use #comment-form as unique jump target, regardless of node type.
  $form['#id'] = drupal_html_id('comment_form');
  $form['#theme'] = array(
    'comment_form__node_' . $node->type,
    'comment_form',
  );
  $anonymous_contact = variable_get('comment_anonymous_' . $node->type, COMMENT_ANONYMOUS_MAYNOT_CONTACT);
  $is_admin = !empty($comment->cid) && user_access('administer comments');
  if (!$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
    $form['#attached']['library'][] = array(
      'system',
      'jquery.cookie',
    );
    $form['#attributes']['class'][] = 'user-info-from-cookie';
  }

  // If not replying to a comment, use our dedicated page callback for new
  // comments on nodes.
  if (empty($comment->cid) && empty($comment->pid)) {
    $form['#action'] = url('comment/reply/' . $comment->nid);
  }
  if (isset($form_state['comment_preview'])) {
    $form += $form_state['comment_preview'];
  }
  $form['author'] = array(
    '#weight' => 10,
  );

  // Display author information in a details element for comment moderators.
  if ($is_admin) {
    $form['author'] += array(
      '#type' => 'details',
      '#title' => t('Administration'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
  }

  // Prepare default values for form elements.
  if ($is_admin) {
    $author = !$comment->uid && $comment->name ? $comment->name : $comment->registered_name;
    $status = isset($comment->status) ? $comment->status : COMMENT_NOT_PUBLISHED;
    $date = !empty($comment->date) ? $comment->date : format_date($comment->created, 'custom', 'Y-m-d H:i O');
  }
  else {
    if ($user->uid) {
      $author = $user->name;
    }
    else {
      $author = $comment->name ? $comment->name : '';
    }
    $status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
    $date = '';
  }

  // Add the author name field depending on the current user.
  if ($is_admin) {
    $form['author']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Authored by'),
      '#default_value' => $author,
      '#maxlength' => 60,
      '#size' => 30,
      '#description' => t('Leave blank for %anonymous.', array(
        '%anonymous' => config('user.settings')
          ->get('anonymous'),
      )),
      '#autocomplete_path' => 'user/autocomplete',
    );
  }
  elseif ($user->uid) {
    $form['author']['_author'] = array(
      '#type' => 'item',
      '#title' => t('Your name'),
      '#markup' => theme('username', array(
        'account' => $user,
      )),
    );
    $form['author']['name'] = array(
      '#type' => 'value',
      '#value' => $author,
    );
  }
  else {
    $form['author']['name'] = array(
      '#type' => 'textfield',
      '#title' => t('Your name'),
      '#default_value' => $author,
      '#required' => !$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT,
      '#maxlength' => 60,
      '#size' => 30,
    );
  }

  // Add author e-mail and homepage fields depending on the current user.
  $form['author']['mail'] = array(
    '#type' => 'email',
    '#title' => t('E-mail'),
    '#default_value' => $comment->mail,
    '#required' => !$user->uid && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT,
    '#maxlength' => 64,
    '#size' => 30,
    '#description' => t('The content of this field is kept private and will not be shown publicly.'),
    '#access' => $is_admin || !$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT,
  );
  $form['author']['homepage'] = array(
    '#type' => 'url',
    '#title' => t('Homepage'),
    '#default_value' => $comment->homepage,
    '#maxlength' => 255,
    '#size' => 30,
    '#access' => $is_admin || !$user->uid && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT,
  );

  // Add administrative comment publishing options.
  $form['author']['date'] = array(
    '#type' => 'textfield',
    '#title' => t('Authored on'),
    '#default_value' => $date,
    '#maxlength' => 25,
    '#size' => 20,
    '#access' => $is_admin,
  );
  $form['author']['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#default_value' => $status,
    '#options' => array(
      COMMENT_PUBLISHED => t('Published'),
      COMMENT_NOT_PUBLISHED => t('Not published'),
    ),
    '#access' => $is_admin,
  );
  $form['subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#maxlength' => 64,
    '#default_value' => $comment->subject,
    '#access' => variable_get('comment_subject_field_' . $node->type, 1) == 1,
  );

  // Used for conditional validation of author fields.
  $form['is_anonymous'] = array(
    '#type' => 'value',
    '#value' => $comment->cid ? !$comment->uid : !$user->uid,
  );

  // Add internal comment properties.
  foreach (array(
    'cid',
    'pid',
    'nid',
    'uid',
  ) as $key) {
    $form[$key] = array(
      '#type' => 'value',
      '#value' => $comment->{$key},
    );
  }
  $form['node_type'] = array(
    '#type' => 'value',
    '#value' => 'comment_node_' . $node->type,
  );

  // Make the comment inherit the current content language unless specifically
  // set.
  if ($comment
    ->isNew()) {
    $language_content = language(LANGUAGE_TYPE_CONTENT);
    $comment->langcode = $language_content->langcode;
  }
  $form['langcode'] = array(
    '#type' => 'value',
    '#value' => $comment->langcode,
  );

  // Attach fields.
  $comment->node_type = 'comment_node_' . $node->type;
  return parent::form($form, $form_state, $comment);
}