CommentForm::form

public CommentForm::form(array $form, FormStateInterface $form_state)

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

core/modules/comment/src/CommentForm.php, line 66

Class

CommentForm
Base handler for comment forms.

Namespace

Drupal\comment

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
public function form(array $form, FormStateInterface $form_state) {
  /** @var \Drupal\comment\CommentInterface $comment */
  $comment = $this->entity;
  $entity = $this->entityManager->getStorage($comment->getCommentedEntityTypeId())->load($comment->getCommentedEntityId());
  $field_name = $comment->getFieldName();
  $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
  $config = $this->config('user.settings');
 
  // In several places within this function, we vary $form on:
  // - The current user's permissions.
  // - Whether the current user is authenticated or anonymous.
  // - The 'user.settings' configuration.
  // - The comment field's definition.
  $form['#cache']['contexts'][] = 'user.permissions';
  $form['#cache']['contexts'][] = 'user.roles:authenticated';
  $this->renderer->addCacheableDependency($form, $config);
  $this->renderer->addCacheableDependency($form, $field_definition->getConfig($entity->bundle()));
 
  // Use #comment-form as unique jump target, regardless of entity type.
  $form['#id'] = Html::getUniqueId('comment_form');
  $form['#theme'] = array('comment_form__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, 'comment_form');
 
  $anonymous_contact = $field_definition->getSetting('anonymous');
  $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments');
 
  if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) {
    $form['#attached']['library'][] = 'core/drupal.form';
    $form['#attributes']['data-user-info-from-browser'] = TRUE;
  }
 
  // If not replying to a comment, use our dedicated page callback for new
  // Comments on entities.
  if (!$comment->id() && !$comment->hasParentComment()) {
    $form['#action'] = $this->url('comment.reply', array('entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id(), 'field_name' => $field_name));
  }
 
  $comment_preview = $form_state->get('comment_preview');
  if (isset($comment_preview)) {
    $form += $comment_preview;
  }
 
  $form['author'] = array();
  // Display author information in a details element for comment moderators.
  if ($is_admin) {
    $form['author'] += array(
      '#type' => 'details',
      '#title' => $this->t('Administration'),
    );
  }
 
  // Prepare default values for form elements.
  $author = '';
  if ($is_admin) {
    if (!$comment->getOwnerId()) {
      $author = $comment->getAuthorName();
    }
    $status = $comment->getStatus();
    if (empty($comment_preview)) {
      $form['#title'] = $this->t('Edit comment %title', array(
        '%title' => $comment->getSubject(),
      ));
    }
  }
  else {
    $status = ($this->currentUser->hasPermission('skip comment approval') ? CommentInterface::PUBLISHED : CommentInterface::NOT_PUBLISHED);
  }
 
  $date = '';
  if ($comment->id()) {
    $date = !empty($comment->date) ? $comment->date : DrupalDateTime::createFromTimestamp($comment->getCreatedTime());
  }
 
  // The uid field is only displayed when a user with the permission
  // 'administer comments' is editing an existing comment from an
  // authenticated user.
  $owner = $comment->getOwner();
  $form['author']['uid'] = [
    '#type' => 'entity_autocomplete',
    '#target_type' => 'user',
    '#default_value' => $owner->isAnonymous() ? NULL : $owner,
    // A comment can be made anonymous by leaving this field empty therefore
    // there is no need to list them in the autocomplete.
    '#selection_settings' => ['include_anonymous' => FALSE],
    '#title' => $this->t('Authored by'),
    '#description' => $this->t('Leave blank for %anonymous.', ['%anonymous' => $config->get('anonymous')]),
    '#access' => $is_admin,
  ];
 
  // The name field is displayed when an anonymous user is adding a comment or
  // when a user with the permission 'administer comments' is editing an
  // existing comment from an anonymous user.
  $form['author']['name'] = array(
    '#type' => 'textfield',
    '#title' => $is_admin ? $this->t('Name for @anonymous', ['@anonymous' => $config->get('anonymous')]) : $this->t('Your name'),
    '#default_value' => $author,
    '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT),
    '#maxlength' => 60,
    '#access' => $this->currentUser->isAnonymous() || $is_admin,
    '#size' => 30,
    '#attributes' => [
      'data-drupal-default-value' => $config->get('anonymous'),
    ],
  );
 
  if ($is_admin) {
    // When editing a comment only display the name textfield if the uid field
    // is empty.
    $form['author']['name']['#states'] = [
      'visible' => [
        ':input[name="uid"]' => array('empty' => TRUE),
      ],
    ];
  }
 
  // Add author email and homepage fields depending on the current user.
  $form['author']['mail'] = array(
    '#type' => 'email',
    '#title' => $this->t('Email'),
    '#default_value' => $comment->getAuthorEmail(),
    '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT),
    '#maxlength' => 64,
    '#size' => 30,
    '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'),
    '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
  );
 
  $form['author']['homepage'] = array(
    '#type' => 'url',
    '#title' => $this->t('Homepage'),
    '#default_value' => $comment->getHomepage(),
    '#maxlength' => 255,
    '#size' => 30,
    '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT),
  );
 
  // Add administrative comment publishing options.
  $form['author']['date'] = array(
    '#type' => 'datetime',
    '#title' => $this->t('Authored on'),
    '#default_value' => $date,
    '#size' => 20,
    '#access' => $is_admin,
  );
 
  $form['author']['status'] = array(
    '#type' => 'radios',
    '#title' => $this->t('Status'),
    '#default_value' => $status,
    '#options' => array(
      CommentInterface::PUBLISHED => $this->t('Published'),
      CommentInterface::NOT_PUBLISHED => $this->t('Not published'),
    ),
    '#access' => $is_admin,
  );
 
  return parent::form($form, $form_state, $comment);
}
doc_Drupal
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.