Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / comment / comment.module
1 <?php
2
3 /**
4  * @file
5  * Enables users to comment on published content.
6  *
7  * When installed, the Comment module creates a field that facilitates a
8  * discussion board for each Drupal entity to which a comment field is attached.
9  * Users can post comments to discuss a forum topic, story, collaborative
10  * book page, user etc.
11  */
12
13 use Drupal\comment\CommentInterface;
14 use Drupal\comment\Entity\CommentType;
15 use Drupal\Core\Entity\FieldableEntityInterface;
16 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
17 use Drupal\Core\Entity\Entity\EntityViewMode;
18 use Drupal\Core\Entity\EntityInterface;
19 use Drupal\Core\Form\FormStateInterface;
20 use Drupal\Core\Routing\RouteMatchInterface;
21 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
22 use Drupal\Core\Render\Element;
23 use Drupal\Core\Url;
24 use Drupal\field\FieldConfigInterface;
25 use Drupal\field\FieldStorageConfigInterface;
26 use Drupal\node\NodeInterface;
27 use Drupal\user\RoleInterface;
28
29 /**
30  * Anonymous posters cannot enter their contact information.
31  *
32  * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
33  *   Use \Drupal\comment\CommentInterface::ANONYMOUS_MAYNOT_CONTACT instead.
34  */
35 const COMMENT_ANONYMOUS_MAYNOT_CONTACT = 0;
36
37 /**
38  * Anonymous posters may leave their contact information.
39  *
40  * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
41  *   Use \Drupal\comment\CommentInterface::ANONYMOUS_MAY_CONTACT instead.
42  */
43 const COMMENT_ANONYMOUS_MAY_CONTACT = 1;
44
45 /**
46  * Anonymous posters are required to leave their contact information.
47  *
48  * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
49  *   Use \Drupal\comment\CommentInterface::ANONYMOUS_MUST_CONTACT instead.
50  */
51 const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
52
53 /**
54  * The time cutoff for comments marked as read for entity types other node.
55  *
56  * Comments changed before this time are always marked as read.
57  * Comments changed after this time may be marked new, updated, or read,
58  * depending on their state for the current user. Defaults to 30 days ago.
59  *
60  * @todo Remove when https://www.drupal.org/node/1029708 lands.
61  */
62 define('COMMENT_NEW_LIMIT', REQUEST_TIME - 30 * 24 * 60 * 60);
63
64 /**
65  * Implements hook_help().
66  */
67 function comment_help($route_name, RouteMatchInterface $route_match) {
68   switch ($route_name) {
69     case 'help.page.comment':
70       $output = '<h3>' . t('About') . '</h3>';
71       $output .= '<p>' . t('The Comment module allows users to comment on site content, set commenting defaults and permissions, and moderate comments. For more information, see the <a href=":comment">online documentation for the Comment module</a>.', [':comment' => 'https://www.drupal.org/documentation/modules/comment']) . '</p>';
72       $output .= '<h3>' . t('Uses') . '</h3>';
73       $output .= '<dl>';
74       $output .= '<dt>' . t('Enabling commenting') . '</dt>';
75       $output .= '<dd>' . t('Comment functionality can be enabled for any entity sub-type (for example, a <a href=":content-type">content type</a>) by adding a <em>Comments</em> field on its <em>Manage fields page</em>. Adding or removing commenting for an entity through the user interface requires the <a href=":field_ui">Field UI</a> module to be enabled, even though the commenting functionality works without it. For more information on fields and entities, see the <a href=":field">Field module help page</a>.', [':content-type' => (\Drupal::moduleHandler()->moduleExists('node')) ? \Drupal::url('entity.node_type.collection') : '#', ':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
76       $output .= '<dt>' . t('Configuring commenting settings') . '</dt>';
77       $output .= '<dd>' . t('Commenting settings can be configured by editing the <em>Comments</em> field on the <em>Manage fields page</em> of an entity type if the <em>Field UI module</em> is enabled. Configuration includes the label of the comments field, the number of comments to be displayed, and whether they are shown in threaded list. Commenting can be be configured as: <em>Open</em> to allow new comments, <em>Closed</em> to view existing comments, but prevent new comments, or <em>Hidden</em> to hide existing comments and prevent new comments. Changing this configuration for an entity type will not change existing entity items.') . '</dd>';
78       $output .= '<dt>' . t('Overriding default settings') . '</dt>';
79       $output .= '<dd>' . t('Users with the appropriate permissions can override the default commenting settings of an entity type when they create an item of that type.') . '</dd>';
80       $output .= '<dt>' . t('Adding comment types') . '</dt>';
81       $output .= '<dd>' . t('Additional <em>comment types</em> can be created per entity sub-type and added on the <a href=":field">Comment types page</a>. If there are multiple comment types available you can select the appropriate one after adding a <em>Comments field</em>.', [':field' => \Drupal::url('entity.comment_type.collection')]) . '</dd>';
82       $output .= '<dt>' . t('Approving and managing comments') . '</dt>';
83       $output .= '<dd>' . t('Comments from users who have the <em>Skip comment approval</em> permission are published immediately. All other comments are placed in the <a href=":comment-approval">Unapproved comments</a> queue, until a user who has permission to <em>Administer comments and comment settings</em> publishes or deletes them. Published comments can be bulk managed on the <a href=":admin-comment">Published comments</a> administration page. When a comment has no replies, it remains editable by its author, as long as the author has <em>Edit own comments</em> permission.', [':comment-approval' => \Drupal::url('comment.admin_approval'), ':admin-comment' => \Drupal::url('comment.admin')]) . '</dd>';
84       $output .= '</dl>';
85       return $output;
86
87     case 'entity.comment_type.collection':
88       $output = '<p>' . t('This page provides a list of all comment types on the site and allows you to manage the fields, form and display settings for each.') . '</p>';
89       return $output;
90   }
91 }
92
93 /**
94  * Entity URI callback.
95  */
96 function comment_uri(CommentInterface $comment) {
97   return new Url(
98     'entity.comment.canonical',
99     [
100       'comment' => $comment->id(),
101     ],
102     ['fragment' => 'comment-' . $comment->id()]
103   );
104 }
105
106 /**
107  * Implements hook_entity_extra_field_info().
108  */
109 function comment_entity_extra_field_info() {
110   $return = [];
111   foreach (CommentType::loadMultiple() as $comment_type) {
112     $return['comment'][$comment_type->id()] = [
113       'form' => [
114         'author' => [
115           'label' => t('Author'),
116           'description' => t('Author textfield'),
117           'weight' => -2,
118         ],
119       ],
120     ];
121     $return['comment'][$comment_type->id()]['display']['links'] = [
122       'label' => t('Links'),
123       'description' => t('Comment operation links'),
124       'weight' => 100,
125       'visible' => TRUE,
126     ];
127   }
128
129   return $return;
130 }
131
132 /**
133  * Implements hook_theme().
134  */
135 function comment_theme() {
136   return [
137     'comment' => [
138       'render element' => 'elements',
139     ],
140     'field__comment' => [
141       'base hook' => 'field',
142     ],
143   ];
144 }
145
146 /**
147  * Implements hook_ENTITY_TYPE_create() for 'field_config'.
148  */
149 function comment_field_config_create(FieldConfigInterface $field) {
150   if ($field->getType() == 'comment' && !$field->isSyncing()) {
151     // Assign default values for the field.
152     $default_value = $field->getDefaultValueLiteral();
153     $default_value += [[]];
154     $default_value[0] += [
155       'status' => CommentItemInterface::OPEN,
156       'cid' => 0,
157       'last_comment_timestamp' => 0,
158       'last_comment_name' => '',
159       'last_comment_uid' => 0,
160       'comment_count' => 0,
161     ];
162     $field->setDefaultValue($default_value);
163   }
164 }
165
166 /**
167  * Implements hook_ENTITY_TYPE_update() for 'field_config'.
168  */
169 function comment_field_config_update(FieldConfigInterface $field) {
170   if ($field->getType() == 'comment') {
171     // Comment field settings also affects the rendering of *comment* entities,
172     // not only the *commented* entities.
173     \Drupal::entityManager()->getViewBuilder('comment')->resetCache();
174   }
175 }
176
177 /**
178  * Implements hook_ENTITY_TYPE_insert() for 'field_storage_config'.
179  */
180 function comment_field_storage_config_insert(FieldStorageConfigInterface $field_storage) {
181   if ($field_storage->getType() == 'comment') {
182     // Check that the target entity type uses an integer ID.
183     $entity_type_id = $field_storage->getTargetEntityTypeId();
184     if (!_comment_entity_uses_integer_id($entity_type_id)) {
185       throw new \UnexpectedValueException('You cannot attach a comment field to an entity with a non-integer ID field');
186     }
187   }
188 }
189
190 /**
191  * Implements hook_ENTITY_TYPE_delete() for 'field_config'.
192  */
193 function comment_field_config_delete(FieldConfigInterface $field) {
194   if ($field->getType() == 'comment') {
195     // Delete all comments that used by the entity bundle.
196     $entity_query = \Drupal::entityQuery('comment');
197     $entity_query->condition('entity_type', $field->getEntityTypeId());
198     $entity_query->condition('field_name', $field->getName());
199     $cids = $entity_query->execute();
200     entity_delete_multiple('comment', $cids);
201   }
202 }
203
204 /**
205  * Implements hook_node_links_alter().
206  */
207 function comment_node_links_alter(array &$links, NodeInterface $node, array &$context) {
208   // Comment links are only added to node entity type for backwards
209   // compatibility. Should you require comment links for other entity types you
210   // can do so by implementing a new field formatter.
211   // @todo Make this configurable from the formatter. See
212   //   https://www.drupal.org/node/1901110.
213
214   $comment_links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
215   $links += $comment_links;
216 }
217
218 /**
219  * Implements hook_entity_view().
220  */
221 function comment_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
222   if ($entity instanceof FieldableEntityInterface && $view_mode == 'rss' && $display->getComponent('links')) {
223     /** @var \Drupal\comment\CommentManagerInterface $comment_manager */
224     $comment_manager = \Drupal::service('comment.manager');
225     $fields = $comment_manager->getFields($entity->getEntityTypeId());
226     foreach ($fields as $field_name => $detail) {
227       if ($entity->hasField($field_name) && $entity->get($field_name)->status != CommentItemInterface::HIDDEN) {
228         // Add a comments RSS element which is a URL to the comments of this
229         // entity.
230         $options = [
231           'fragment' => 'comments',
232           'absolute' => TRUE,
233         ];
234         $entity->rss_elements[] = [
235           'key' => 'comments',
236           'value' => $entity->url('canonical', $options),
237         ];
238       }
239     }
240   }
241 }
242
243 /**
244  * Implements hook_ENTITY_TYPE_view_alter() for node entities.
245  */
246 function comment_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
247   if (\Drupal::moduleHandler()->moduleExists('history')) {
248     $build['#attributes']['data-history-node-id'] = $node->id();
249   }
250 }
251
252 /**
253  * Generates an array for rendering a comment.
254  *
255  * @param \Drupal\comment\CommentInterface $comment
256  *   The comment object.
257  * @param string $view_mode
258  *   (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
259  *   'full'.
260  * @param string $langcode
261  *   (optional) A language code to use for rendering. Defaults to the global
262  *   content language of the current request.
263  *
264  * @return array
265  *   An array as expected by drupal_render().
266  *
267  * @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
268  *   Use \Drupal::entityManager()->getViewBuilder('comment')->view().
269  */
270 function comment_view(CommentInterface $comment, $view_mode = 'full', $langcode = NULL) {
271   return entity_view($comment, $view_mode, $langcode);
272 }
273
274 /**
275  * Constructs render array from an array of loaded comments.
276  *
277  * @param \Drupal\comment\CommentInterface[] $comments
278  *   An array of comments as returned by entity_load_multiple().
279  * @param string $view_mode
280  *   (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
281  *   'full'.
282  * @param string $langcode
283  *   (optional) A string indicating the language field values are to be shown
284  *   in. If no language is provided the current content language is used.
285  *   Defaults to NULL.
286  *
287  * @return array
288  *   An array in the format expected by drupal_render().
289  *
290  * @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
291  *   Use \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple().
292  *
293  * @see drupal_render()
294  */
295 function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) {
296   return entity_view_multiple($comments, $view_mode, $langcode);
297 }
298
299 /**
300  * Implements hook_form_FORM_ID_alter() for field_ui_field_storage_add_form.
301  */
302 function comment_form_field_ui_field_storage_add_form_alter(&$form, FormStateInterface $form_state) {
303   $route_match = \Drupal::routeMatch();
304   if ($form_state->get('entity_type_id') == 'comment' && $route_match->getParameter('commented_entity_type')) {
305     $form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
306   }
307   if (!_comment_entity_uses_integer_id($form_state->get('entity_type_id'))) {
308     $optgroup = (string) t('General');
309     // You cannot use comment fields on entity types with non-integer IDs.
310     unset($form['add']['new_storage_type']['#options'][$optgroup]['comment']);
311   }
312 }
313
314 /**
315  * Implements hook_form_FORM_ID_alter().
316  */
317 function comment_form_field_ui_form_display_overview_form_alter(&$form, FormStateInterface $form_state) {
318   $route_match = \Drupal::routeMatch();
319   if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
320     $form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
321   }
322 }
323
324 /**
325  * Implements hook_form_FORM_ID_alter().
326  */
327 function comment_form_field_ui_display_overview_form_alter(&$form, FormStateInterface $form_state) {
328   $route_match = \Drupal::routeMatch();
329   if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
330     $form['#title'] = \Drupal::service('comment.manager')->getFieldUIPageTitle($route_match->getParameter('commented_entity_type'), $route_match->getParameter('field_name'));
331   }
332 }
333
334 /**
335  * Implements hook_form_FORM_ID_alter() for 'field_storage_config_edit_form'.
336  */
337 function comment_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state) {
338   if ($form_state->getFormObject()->getEntity()->getType() == 'comment') {
339     // We only support posting one comment at the time so it doesn't make sense
340     // to let the site builder choose anything else.
341     $form['cardinality_container']['cardinality']['#default_value'] = 1;
342     $form['cardinality_container']['#access'] = FALSE;
343   }
344 }
345
346 /**
347  * Implements hook_entity_storage_load().
348  *
349  * @see \Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()
350  */
351 function comment_entity_storage_load($entities, $entity_type) {
352   // Comments can only be attached to content entities, so skip others.
353   if (!\Drupal::entityManager()->getDefinition($entity_type)->entityClassImplements(FieldableEntityInterface::class)) {
354     return;
355   }
356   // @todo Investigate in https://www.drupal.org/node/2527866 why we need that.
357   if (!\Drupal::hasService('comment.manager') || !\Drupal::service('comment.manager')->getFields($entity_type)) {
358     // Do not query database when entity has no comment fields.
359     return;
360   }
361   // Load comment information from the database and update the entity's
362   // comment statistics properties, which are defined on each CommentItem field.
363   $result = \Drupal::service('comment.statistics')->read($entities, $entity_type);
364   foreach ($result as $record) {
365     // Skip fields that entity does not have.
366     if (!$entities[$record->entity_id]->hasField($record->field_name)) {
367       continue;
368     }
369     $comment_statistics = $entities[$record->entity_id]->get($record->field_name);
370     $comment_statistics->cid = $record->cid;
371     $comment_statistics->last_comment_timestamp = $record->last_comment_timestamp;
372     $comment_statistics->last_comment_name = $record->last_comment_name;
373     $comment_statistics->last_comment_uid = $record->last_comment_uid;
374     $comment_statistics->comment_count = $record->comment_count;
375   }
376 }
377
378 /**
379  * Implements hook_entity_insert().
380  */
381 function comment_entity_insert(EntityInterface $entity) {
382   // Allow bulk updates and inserts to temporarily disable the
383   // maintenance of the {comment_entity_statistics} table.
384   if (\Drupal::state()->get('comment.maintain_entity_statistics') &&
385     $fields = \Drupal::service('comment.manager')->getFields($entity->getEntityTypeId())) {
386     \Drupal::service('comment.statistics')->create($entity, $fields);
387   }
388 }
389
390 /**
391  * Implements hook_entity_predelete().
392  */
393 function comment_entity_predelete(EntityInterface $entity) {
394   // Entities can have non-numeric IDs, but {comment} and
395   // {comment_entity_statistics} tables have integer columns for entity ID, and
396   // PostgreSQL throws exceptions if you attempt query conditions with
397   // mismatched types. So, we need to verify that the ID is numeric (even for an
398   // entity type that has an integer ID, $entity->id() might be a string
399   // containing a number), and then cast it to an integer when querying.
400   if ($entity instanceof FieldableEntityInterface && is_numeric($entity->id())) {
401     $entity_query = \Drupal::entityQuery('comment');
402     $entity_query->condition('entity_id', (int) $entity->id());
403     $entity_query->condition('entity_type', $entity->getEntityTypeId());
404     $cids = $entity_query->execute();
405     entity_delete_multiple('comment', $cids);
406     \Drupal::service('comment.statistics')->delete($entity);
407   }
408 }
409
410 /**
411  * Determines if an entity type is using an integer-based ID definition.
412  *
413  * @param string $entity_type_id
414  *   The ID the represents the entity type.
415  *
416  * @return bool
417  *   Returns TRUE if the entity type has an integer-based ID definition and
418  *   FALSE otherwise.
419  */
420 function _comment_entity_uses_integer_id($entity_type_id) {
421   $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
422   $entity_type_id_key = $entity_type->getKey('id');
423   if ($entity_type_id_key === FALSE) {
424     return FALSE;
425   }
426   $field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type->id());
427   $entity_type_id_definition = $field_definitions[$entity_type_id_key];
428   return $entity_type_id_definition->getType() === 'integer';
429 }
430
431 /**
432  * Implements hook_node_update_index().
433  */
434 function comment_node_update_index(EntityInterface $node) {
435   $index_comments = &drupal_static(__FUNCTION__);
436
437   if ($index_comments === NULL) {
438     // Do not index in the following three cases:
439     // 1. 'Authenticated user' can search content but can't access comments.
440     // 2. 'Anonymous user' can search content but can't access comments.
441     // 3. Any role can search content but can't access comments and access
442     // comments is not granted by the 'authenticated user' role. In this case
443     // all users might have both permissions from various roles but it is also
444     // possible to set up a user to have only search content and so a user
445     // edit could change the security situation so it is not safe to index the
446     // comments.
447     $index_comments = TRUE;
448     $roles = \Drupal::entityManager()->getStorage('user_role')->loadMultiple();
449     $authenticated_can_access = $roles[RoleInterface::AUTHENTICATED_ID]->hasPermission('access comments');
450     foreach ($roles as $rid => $role) {
451       if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
452         if ($rid == RoleInterface::AUTHENTICATED_ID || $rid == RoleInterface::ANONYMOUS_ID || !$authenticated_can_access) {
453           $index_comments = FALSE;
454           break;
455         }
456       }
457     }
458   }
459
460   $build = [];
461
462   if ($index_comments) {
463     foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) {
464       // Skip fields that entity does not have.
465       if (!$node->hasField($field_name)) {
466         continue;
467       }
468       $field_definition = $node->getFieldDefinition($field_name);
469       $mode = $field_definition->getSetting('default_mode');
470       $comments_per_page = $field_definition->getSetting('per_page');
471       if ($node->get($field_name)->status) {
472         $comments = \Drupal::entityManager()->getStorage('comment')
473           ->loadThread($node, $field_name, $mode, $comments_per_page);
474         if ($comments) {
475           $build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
476         }
477       }
478     }
479   }
480   return \Drupal::service('renderer')->renderPlain($build);
481 }
482
483 /**
484  * Implements hook_cron().
485  */
486 function comment_cron() {
487   // Store the maximum possible comments per thread (used for node search
488   // ranking by reply count).
489   \Drupal::state()->set('comment.node_comment_statistics_scale', 1.0 / max(1, \Drupal::service('comment.statistics')->getMaximumCount('node')));
490 }
491
492 /**
493  * Implements hook_node_search_result().
494  *
495  * Formats a comment count string and returns it, for display with search
496  * results.
497  */
498 function comment_node_search_result(EntityInterface $node) {
499   $comment_fields = \Drupal::service('comment.manager')->getFields('node');
500   $comments = 0;
501   $open = FALSE;
502   foreach ($comment_fields as $field_name => $info) {
503     // Skip fields that entity does not have.
504     if (!$node->hasField($field_name)) {
505       continue;
506     }
507     // Do not make a string if comments are hidden.
508     $status = $node->get($field_name)->status;
509     if (\Drupal::currentUser()->hasPermission('access comments') && $status != CommentItemInterface::HIDDEN) {
510       if ($status == CommentItemInterface::OPEN) {
511         // At least one comment field is open.
512         $open = TRUE;
513       }
514       $comments += $node->get($field_name)->comment_count;
515     }
516   }
517   // Do not make a string if there are no comment fields, or no comments exist
518   // or all comment fields are hidden.
519   if ($comments > 0 || $open) {
520     return ['comment' => \Drupal::translation()->formatPlural($comments, '1 comment', '@count comments')];
521   }
522 }
523
524 /**
525  * Implements hook_user_cancel().
526  */
527 function comment_user_cancel($edit, $account, $method) {
528   switch ($method) {
529     case 'user_cancel_block_unpublish':
530       $comments = entity_load_multiple_by_properties('comment', ['uid' => $account->id()]);
531       foreach ($comments as $comment) {
532         $comment->setPublished(CommentInterface::NOT_PUBLISHED);
533         $comment->save();
534       }
535       break;
536
537     case 'user_cancel_reassign':
538       /** @var \Drupal\comment\CommentInterface[] $comments */
539       $comments = entity_load_multiple_by_properties('comment', ['uid' => $account->id()]);
540       foreach ($comments as $comment) {
541         $comment->setOwnerId(0);
542         $comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));
543         $comment->save();
544       }
545       break;
546   }
547 }
548
549 /**
550  * Implements hook_ENTITY_TYPE_predelete() for user entities.
551  */
552 function comment_user_predelete($account) {
553   $entity_query = \Drupal::entityQuery('comment');
554   $entity_query->condition('uid', $account->id());
555   $cids = $entity_query->execute();
556   entity_delete_multiple('comment', $cids);
557 }
558
559 /**
560  * Generates a comment preview.
561  *
562  * @param \Drupal\comment\CommentInterface $comment
563  *   The comment entity to preview.
564  * @param Drupal\Core\Form\FormStateInterface $form_state
565  *   The current state of the form.
566  *
567  * @return array
568  *   An array as expected by drupal_render().
569  */
570 function comment_preview(CommentInterface $comment, FormStateInterface $form_state) {
571   $preview_build = [];
572   $entity = $comment->getCommentedEntity();
573
574   if (!$form_state->getErrors()) {
575     $comment->in_preview = TRUE;
576     $comment_build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($comment);
577     $comment_build['#weight'] = -100;
578
579     $preview_build['comment_preview'] = $comment_build;
580   }
581
582   if ($comment->hasParentComment()) {
583     $build = [];
584     $parent = $comment->getParentComment();
585     if ($parent && $parent->isPublished()) {
586       $build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($parent);
587     }
588   }
589   else {
590     // The comment field output includes rendering the parent entity of the
591     // thread to which the comment is a reply. The rendered entity output
592     // includes the comment reply form, which contains the comment preview and
593     // therefore the rendered parent entity. This results in an infinite loop of
594     // parent entity output rendering the comment form and the comment form
595     // rendering the parent entity. To prevent this infinite loop we temporarily
596     // set the value of the comment field on a clone of the entity to hidden
597     // before calling entity_view(). That way when the output of the commented
598     // entity is rendered, it excludes the comment field output.
599     $field_name = $comment->getFieldName();
600     $entity = clone $entity;
601     $entity->$field_name->status = CommentItemInterface::HIDDEN;
602     $build = entity_view($entity, 'full');
603   }
604
605   $preview_build['comment_output_below'] = $build;
606   $preview_build['comment_output_below']['#weight'] = 100;
607
608   return $preview_build;
609 }
610
611 /**
612  * Implements hook_preprocess_HOOK() for block templates.
613  */
614 function comment_preprocess_block(&$variables) {
615   if ($variables['configuration']['provider'] == 'comment') {
616     $variables['attributes']['role'] = 'navigation';
617   }
618 }
619
620 /**
621  * Prepares variables for comment templates.
622  *
623  * Default template: comment.html.twig.
624  *
625  * @param array $variables
626  *   An associative array containing:
627  *   - elements: An associative array containing the comment and entity objects.
628  *     Array keys: #comment, #commented_entity.
629  */
630 function template_preprocess_comment(&$variables) {
631   /** @var \Drupal\comment\CommentInterface $comment */
632   $comment = $variables['elements']['#comment'];
633   $commented_entity = $comment->getCommentedEntity();
634   $variables['comment'] = $comment;
635   $variables['commented_entity'] = $commented_entity;
636   $variables['threaded'] = $variables['elements']['#comment_threaded'];
637
638   $account = $comment->getOwner();
639   $username = [
640     '#theme' => 'username',
641     '#account' => $account,
642   ];
643   $variables['author'] = drupal_render($username);
644   $variables['author_id'] = $comment->getOwnerId();
645   $variables['new_indicator_timestamp'] = $comment->getChangedTime();
646   $variables['created'] = format_date($comment->getCreatedTime());
647   // Avoid calling format_date() twice on the same timestamp.
648   if ($comment->getChangedTime() == $comment->getCreatedTime()) {
649     $variables['changed'] = $variables['created'];
650   }
651   else {
652     $variables['changed'] = format_date($comment->getChangedTime());
653   }
654
655   if (theme_get_setting('features.comment_user_picture')) {
656     // To change user picture settings (for instance, image style), edit the
657     // 'compact' view mode on the User entity.
658     $variables['user_picture'] = user_view($account, 'compact');
659   }
660   else {
661     $variables['user_picture'] = [];
662   }
663
664   if (isset($comment->in_preview)) {
665     $variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>'));
666     $variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>'));
667   }
668   else {
669     $uri = $comment->permalink();
670     $attributes = $uri->getOption('attributes') ?: [];
671     $attributes += ['class' => ['permalink'], 'rel' => 'bookmark'];
672     $uri->setOption('attributes', $attributes);
673     $variables['title'] = \Drupal::l($comment->getSubject(), $uri);
674
675     $variables['permalink'] = \Drupal::l(t('Permalink'), $comment->permalink());
676   }
677
678   $variables['submitted'] = t('Submitted by @username on @datetime', ['@username' => $variables['author'], '@datetime' => $variables['created']]);
679
680   if ($comment->hasParentComment()) {
681     // Fetch and store the parent comment information for use in templates.
682     $comment_parent = $comment->getParentComment();
683     $account_parent = $comment_parent->getOwner();
684     $variables['parent_comment'] = $comment_parent;
685     $username = [
686       '#theme' => 'username',
687       '#account' => $account_parent,
688     ];
689     $variables['parent_author'] = drupal_render($username);
690     $variables['parent_created'] = format_date($comment_parent->getCreatedTime());
691     // Avoid calling format_date() twice on the same timestamp.
692     if ($comment_parent->getChangedTime() == $comment_parent->getCreatedTime()) {
693       $variables['parent_changed'] = $variables['parent_created'];
694     }
695     else {
696       $variables['parent_changed'] = format_date($comment_parent->getChangedTime());
697     }
698     $permalink_uri_parent = $comment_parent->permalink();
699     $attributes = $permalink_uri_parent->getOption('attributes') ?: [];
700     $attributes += ['class' => ['permalink'], 'rel' => 'bookmark'];
701     $permalink_uri_parent->setOption('attributes', $attributes);
702     $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent);
703     $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent);
704     $variables['parent'] = t('In reply to @parent_title by @parent_username',
705         ['@parent_username' => $variables['parent_author'], '@parent_title' => $variables['parent_title']]);
706   }
707   else {
708     $variables['parent_comment'] = '';
709     $variables['parent_author'] = '';
710     $variables['parent_created'] = '';
711     $variables['parent_changed'] = '';
712     $variables['parent_title'] = '';
713     $variables['parent_permalink'] = '';
714     $variables['parent'] = '';
715   }
716
717   // Helpful $content variable for templates.
718   foreach (Element::children($variables['elements']) as $key) {
719     $variables['content'][$key] = $variables['elements'][$key];
720   }
721
722   // Set status to a string representation of comment->status.
723   if (isset($comment->in_preview)) {
724     $variables['status'] = 'preview';
725   }
726   else {
727     $variables['status'] = $comment->isPublished() ? 'published' : 'unpublished';
728   }
729
730   // Add comment author user ID. Necessary for the comment-by-viewer library.
731   $variables['attributes']['data-comment-user-id'] = $comment->getOwnerId();
732 }
733
734 /**
735  * Prepares variables for comment field templates.
736  *
737  * Default template: field--comment.html.twig.
738  *
739  * @param array $variables
740  *   An associative array containing:
741  *   - element: An associative array containing render arrays for the list of
742  *     comments, and the comment form. Array keys: comments, comment_form.
743  *
744  * @todo Rename to template_preprocess_field__comment() once
745  *   https://www.drupal.org/node/939462 is resolved.
746  */
747 function comment_preprocess_field(&$variables) {
748   $element = $variables['element'];
749   if ($element['#field_type'] == 'comment') {
750     // Provide contextual information.
751     $variables['comment_display_mode'] = $element[0]['#comment_display_mode'];
752     $variables['comment_type'] = $element[0]['#comment_type'];
753
754     // Append additional attributes (eg. RDFa) from the first field item.
755     $variables['attributes'] += $variables['items'][0]['attributes']->storage();
756
757     // Create separate variables for the comments and comment form.
758     $variables['comments'] = $element[0]['comments'];
759     $variables['comment_form'] = $element[0]['comment_form'];
760   }
761 }
762
763 /**
764  * Implements hook_ranking().
765  */
766 function comment_ranking() {
767   return \Drupal::service('comment.statistics')->getRankingInfo();
768 }
769
770 /**
771  * Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
772  */
773 function comment_entity_view_display_presave(EntityViewDisplayInterface $display) {
774   // Act only on comment view displays being disabled.
775   if ($display->isNew() || $display->getTargetEntityTypeId() !== 'comment' || $display->status()) {
776     return;
777   }
778   $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
779   if (!$storage->loadUnchanged($display->getOriginalId())->status()) {
780     return;
781   }
782
783   // Disable the comment field formatter when the used view display is disabled.
784   foreach ($storage->loadMultiple() as $id => $view_display) {
785     $changed = FALSE;
786     /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
787     foreach ($view_display->getComponents() as $field => $component) {
788       if (isset($component['type']) && ($component['type'] === 'comment_default')) {
789         if ($component['settings']['view_mode'] === $display->getMode()) {
790           $view_display->removeComponent($field);
791           /** @var \Drupal\Core\Entity\EntityViewModeInterface $mode */
792           $mode = EntityViewMode::load($display->getTargetEntityTypeId() . '.' . $display->getMode());
793           $arguments = [
794             '@id' => $view_display->id(),
795             '@name' => $field,
796             '@display' => $mode->label(),
797             '@mode' => $display->getMode(),
798           ];
799           \Drupal::logger('system')->warning("View display '@id': Comment field formatter '@name' was disabled because it is using the comment view display '@display' (@mode) that was just disabled.", $arguments);
800           $changed = TRUE;
801         }
802       }
803     }
804     if ($changed) {
805       $view_display->save();
806     }
807   }
808 }