public function CommentFormController::form

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

Overrides EntityFormControllerNG::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) {
  global $user;
  $comment = $this->entity;
  $node = $comment->nid->entity;

  // 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 = $comment
    ->id() && 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 (!$comment
    ->id() && !$comment->pid->target_id) {
    $form['#action'] = url('comment/reply/' . $comment->nid->target_id);
  }
  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'),
      '#collapsed' => TRUE,
    );
  }

  // Prepare default values for form elements.
  if ($is_admin) {
    $author = $comment->name->value;
    $status = isset($comment->status->value) ? $comment->status->value : COMMENT_NOT_PUBLISHED;
    $date = !empty($comment->date) ? $comment->date : new DrupalDateTime($comment->created->value);
  }
  else {
    if ($user->uid) {
      $author = $user->name;
    }
    else {
      $author = $comment->name->value ? $comment->name->value : '';
    }
    $status = user_access('skip comment approval') ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
    $date = '';
  }

  // Add the author name field depending on the current user.
  $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,
  );
  if ($is_admin) {
    $form['author']['name']['#title'] = t('Authored by');
    $form['author']['name']['#description'] = t('Leave blank for %anonymous.', array(
      '%anonymous' => config('user.settings')
        ->get('anonymous'),
    ));
    $form['author']['name']['#autocomplete_path'] = 'user/autocomplete';
  }
  elseif ($user->uid) {
    $form['author']['name']['#type'] = 'item';
    $form['author']['name']['#value'] = $form['author']['name']['#default_value'];
    $form['author']['name']['#markup'] = theme('username', array(
      'account' => $user,
    ));
  }

  // 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->value,
    '#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->value,
    '#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' => 'datetime',
    '#title' => t('Authored on'),
    '#default_value' => $date,
    '#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->value,
    '#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
      ->id() ? !$comment->uid->target_id : !$user->uid,
  );

  // Make the comment inherit the current content language unless specifically
  // set.
  if ($comment
    ->isNew()) {
    $language_content = language(Language::TYPE_CONTENT);
    $comment->langcode->value = $language_content->langcode;
  }

  // Add internal comment properties.
  foreach (array(
    'cid',
    'pid',
    'nid',
    'uid',
    'node_type',
    'langcode',
  ) as $key) {
    $key_name = key($comment->{$key}
      ->offsetGet(0)
      ->getPropertyDefinitions());
    $form[$key] = array(
      '#type' => 'value',
      '#value' => $comment->{$key}->{$key_name},
    );
  }
  return parent::form($form, $form_state, $comment);
}