comparison core/modules/comment/src/CommentForm.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
106 $form['#theme'] = ['comment_form__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, 'comment_form']; 106 $form['#theme'] = ['comment_form__' . $entity->getEntityTypeId() . '__' . $entity->bundle() . '__' . $field_name, 'comment_form'];
107 107
108 $anonymous_contact = $field_definition->getSetting('anonymous'); 108 $anonymous_contact = $field_definition->getSetting('anonymous');
109 $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments'); 109 $is_admin = $comment->id() && $this->currentUser->hasPermission('administer comments');
110 110
111 if (!$this->currentUser->isAuthenticated() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT) { 111 if (!$this->currentUser->isAuthenticated() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT) {
112 $form['#attached']['library'][] = 'core/drupal.form'; 112 $form['#attached']['library'][] = 'core/drupal.form';
113 $form['#attributes']['data-user-info-from-browser'] = TRUE; 113 $form['#attributes']['data-user-info-from-browser'] = TRUE;
114 } 114 }
115 115
116 // If not replying to a comment, use our dedicated page callback for new 116 // If not replying to a comment, use our dedicated page callback for new
176 // existing comment from an anonymous user. 176 // existing comment from an anonymous user.
177 $form['author']['name'] = [ 177 $form['author']['name'] = [
178 '#type' => 'textfield', 178 '#type' => 'textfield',
179 '#title' => $is_admin ? $this->t('Name for @anonymous', ['@anonymous' => $config->get('anonymous')]) : $this->t('Your name'), 179 '#title' => $is_admin ? $this->t('Name for @anonymous', ['@anonymous' => $config->get('anonymous')]) : $this->t('Your name'),
180 '#default_value' => $author, 180 '#default_value' => $author,
181 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), 181 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT),
182 '#maxlength' => 60, 182 '#maxlength' => 60,
183 '#access' => $this->currentUser->isAnonymous() || $is_admin, 183 '#access' => $this->currentUser->isAnonymous() || $is_admin,
184 '#size' => 30, 184 '#size' => 30,
185 '#attributes' => [ 185 '#attributes' => [
186 'data-drupal-default-value' => $config->get('anonymous'), 186 'data-drupal-default-value' => $config->get('anonymous'),
200 // Add author email and homepage fields depending on the current user. 200 // Add author email and homepage fields depending on the current user.
201 $form['author']['mail'] = [ 201 $form['author']['mail'] = [
202 '#type' => 'email', 202 '#type' => 'email',
203 '#title' => $this->t('Email'), 203 '#title' => $this->t('Email'),
204 '#default_value' => $comment->getAuthorEmail(), 204 '#default_value' => $comment->getAuthorEmail(),
205 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == COMMENT_ANONYMOUS_MUST_CONTACT), 205 '#required' => ($this->currentUser->isAnonymous() && $anonymous_contact == CommentInterface::ANONYMOUS_MUST_CONTACT),
206 '#maxlength' => 64, 206 '#maxlength' => 64,
207 '#size' => 30, 207 '#size' => 30,
208 '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'), 208 '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'),
209 '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), 209 '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT),
210 ]; 210 ];
211 211
212 $form['author']['homepage'] = [ 212 $form['author']['homepage'] = [
213 '#type' => 'url', 213 '#type' => 'url',
214 '#title' => $this->t('Homepage'), 214 '#title' => $this->t('Homepage'),
215 '#default_value' => $comment->getHomepage(), 215 '#default_value' => $comment->getHomepage(),
216 '#maxlength' => 255, 216 '#maxlength' => 255,
217 '#size' => 30, 217 '#size' => 30,
218 '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), 218 '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != CommentInterface::ANONYMOUS_MAYNOT_CONTACT),
219 ]; 219 ];
220 220
221 // Add administrative comment publishing options. 221 // Add administrative comment publishing options.
222 $form['author']['date'] = [ 222 $form['author']['date'] = [
223 '#type' => 'datetime', 223 '#type' => 'datetime',
247 protected function actions(array $form, FormStateInterface $form_state) { 247 protected function actions(array $form, FormStateInterface $form_state) {
248 $element = parent::actions($form, $form_state); 248 $element = parent::actions($form, $form_state);
249 /* @var \Drupal\comment\CommentInterface $comment */ 249 /* @var \Drupal\comment\CommentInterface $comment */
250 $comment = $this->entity; 250 $comment = $this->entity;
251 $entity = $comment->getCommentedEntity(); 251 $entity = $comment->getCommentedEntity();
252 $field_definition = $this->entityManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()]; 252 $field_definition = $this->entityFieldManager->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle())[$comment->getFieldName()];
253 $preview_mode = $field_definition->getSetting('preview'); 253 $preview_mode = $field_definition->getSetting('preview');
254 254
255 // No delete action on the comment form. 255 // No delete action on the comment form.
256 unset($element['delete']); 256 unset($element['delete']);
257 257
367 */ 367 */
368 public function save(array $form, FormStateInterface $form_state) { 368 public function save(array $form, FormStateInterface $form_state) {
369 $comment = $this->entity; 369 $comment = $this->entity;
370 $entity = $comment->getCommentedEntity(); 370 $entity = $comment->getCommentedEntity();
371 $field_name = $comment->getFieldName(); 371 $field_name = $comment->getFieldName();
372 $uri = $entity->urlInfo(); 372 $uri = $entity->toUrl();
373 $logger = $this->logger('comment'); 373 $logger = $this->logger('comment');
374 374
375 if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) { 375 if ($this->currentUser->hasPermission('post comments') && ($this->currentUser->hasPermission('administer comments') || $entity->{$field_name}->status == CommentItemInterface::OPEN)) {
376 $comment->save(); 376 $comment->save();
377 $form_state->setValue('cid', $comment->id()); 377 $form_state->setValue('cid', $comment->id());
378 378
379 // Add a log entry. 379 // Add a log entry.
380 $logger->notice('Comment posted: %subject.', [ 380 $logger->notice('Comment posted: %subject.', [
381 '%subject' => $comment->getSubject(), 381 '%subject' => $comment->getSubject(),
382 'link' => $this->l(t('View'), $comment->urlInfo()->setOption('fragment', 'comment-' . $comment->id())), 382 'link' => $this->l(t('View'), $comment->toUrl()->setOption('fragment', 'comment-' . $comment->id())),
383 ]); 383 ]);
384 384
385 // Explain the approval queue if necessary. 385 // Explain the approval queue if necessary.
386 if (!$comment->isPublished()) { 386 if (!$comment->isPublished()) {
387 if (!$this->currentUser->hasPermission('administer comments')) { 387 if (!$this->currentUser->hasPermission('administer comments')) {