comment.module 32.8 KB
Newer Older
1
<?php
Dries's avatar
Dries committed
2

Dries's avatar
Dries committed
3 4
/**
 * @file
5
 * Enables users to comment on published content.
Dries's avatar
Dries committed
6
 *
7
 * When installed, the Comment module creates a field that facilitates a
8 9 10
 * discussion board for each Drupal entity to which a comment field is attached.
 * Users can post comments to discuss a forum topic, story, collaborative
 * book page, user etc.
Dries's avatar
Dries committed
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 33
 *
 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
 *   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 41
 *
 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
 *   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 49
 *
 * @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0.
 *   Use \Drupal\comment\CommentInterface::ANONYMOUS_MUST_CONTACT instead.
50
 */
51
const COMMENT_ANONYMOUS_MUST_CONTACT = 2;
52

53 54 55 56 57 58 59
/**
 * The time cutoff for comments marked as read for entity types other node.
 *
 * Comments changed before this time are always marked as read.
 * Comments changed after this time may be marked new, updated, or read,
 * depending on their state for the current user. Defaults to 30 days ago.
 *
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 69
  switch ($route_name) {
    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>.', array(':comment' => 'https://www.drupal.org/documentation/modules/comment')) . '</p>';
72 73
      $output .= '<h3>' . t('Uses') . '</h3>';
      $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>.', array(':content-type' => (\Drupal::moduleHandler()->moduleExists('node')) ? \Drupal::url('entity.node_type.collection') : '#', ':field' => \Drupal::url('help.page', array('name' => 'field')), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', array('name' => 'field_ui')) : '#')) . '</dd>';
76 77
      $output .= '<dt>' . t('Configuring commenting settings') . '</dt>';
      $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 81
      $output .= '<dt>' . t('Adding comment types') . '</dt>';
      $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>.', array(':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.', array(':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
    array(
100 101
      'comment' => $comment->id(),
    ),
102
    array('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 = array();
111
  foreach (CommentType::loadMultiple() as $comment_type) {
112
    $return['comment'][$comment_type->id()] = array(
113 114 115 116 117
      'form' => array(
        'author' => array(
          'label' => t('Author'),
          'description' => t('Author textfield'),
          'weight' => -2,
118
        ),
119 120
      ),
    );
121 122 123 124 125 126
    $return['comment'][$comment_type->id()]['display']['links'] = array(
      'label' => t('Links'),
      'description' => t('Comment operation links'),
      'weight' => 100,
      'visible' => TRUE,
    );
127 128 129 130 131
  }

  return $return;
}

132
/**
133
 * Implements hook_theme().
134 135 136 137
 */
function comment_theme() {
  return array(
    'comment' => array(
138
      'render element' => 'elements',
139
    ),
140 141
    'field__comment' => array(
      'base hook' => 'field',
142 143 144 145
    ),
  );
}

146
/**
147
 * Implements hook_ENTITY_TYPE_create() for 'field_config'.
148
 */
149 150 151
function comment_field_config_create(FieldConfigInterface $field) {
  if ($field->getType() == 'comment' && !$field->isSyncing()) {
    // Assign default values for the field.
152 153 154
    $default_value = $field->getDefaultValueLiteral();
    $default_value += array(array());
    $default_value[0] += array(
155
      'status' => CommentItemInterface::OPEN,
156 157 158 159 160
      'cid' => 0,
      'last_comment_timestamp' => 0,
      'last_comment_name' => '',
      'last_comment_uid' => 0,
      'comment_count' => 0,
161
    );
162
    $field->setDefaultValue($default_value);
163
  }
164
}
165

166
/**
167
 * Implements hook_ENTITY_TYPE_update() for 'field_config'.
168
 */
169 170
function comment_field_config_update(FieldConfigInterface $field) {
  if ($field->getType() == 'comment') {
171 172 173
    // Comment field settings also affects the rendering of *comment* entities,
    // not only the *commented* entities.
    \Drupal::entityManager()->getViewBuilder('comment')->resetCache();
174 175
  }
}
176

177
/**
178
 * Implements hook_ENTITY_TYPE_insert() for 'field_storage_config'.
179
 */
180 181
function comment_field_storage_config_insert(FieldStorageConfigInterface $field_storage) {
  if ($field_storage->getType() == 'comment') {
182
    // Check that the target entity type uses an integer ID.
183
    $entity_type_id = $field_storage->getTargetEntityTypeId();
184 185 186
    if (!_comment_entity_uses_integer_id($entity_type_id)) {
      throw new \UnexpectedValueException('You cannot attach a comment field to an entity with a non-integer ID field');
    }
187 188 189
  }
}

190
/**
191
 * Implements hook_ENTITY_TYPE_delete() for 'field_config'.
192
 */
193 194
function comment_field_config_delete(FieldConfigInterface $field) {
  if ($field->getType() == 'comment') {
195
    // Delete all comments that used by the entity bundle.
196
    $entity_query = \Drupal::entityQuery('comment');
197 198
    $entity_query->condition('entity_type', $field->getEntityTypeId());
    $entity_query->condition('field_name', $field->getName());
199 200
    $cids = $entity_query->execute();
    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 209 210
  // Comment links are only added to node entity type for backwards
  // compatibility. Should you require comment links for other entity types you
  // can do so by implementing a new field formatter.
211 212
  // @todo Make this configurable from the formatter. See
  //   https://www.drupal.org/node/1901110.
213

214 215
  $comment_links = \Drupal::service('comment.link_builder')->buildCommentedEntityLinks($node, $context);
  $links += $comment_links;
216 217
}

218 219 220
/**
 * Implements hook_entity_view().
 */
221
function comment_entity_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  if ($entity instanceof FieldableEntityInterface && $view_mode == 'rss' && $display->getComponent('links')) {
    /** @var \Drupal\comment\CommentManagerInterface $comment_manager */
    $comment_manager = \Drupal::service('comment.manager');
    $fields = $comment_manager->getFields($entity->getEntityTypeId());
    foreach ($fields as $field_name => $detail) {
      if ($entity->hasField($field_name) && $entity->get($field_name)->status != CommentItemInterface::HIDDEN) {
        // Add a comments RSS element which is a URL to the comments of this
        // entity.
        $options = array(
          'fragment' => 'comments',
          'absolute' => TRUE,
        );
        $entity->rss_elements[] = array(
          'key' => 'comments',
          'value' => $entity->url('canonical', $options),
        );
      }
    }
  }
}

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 249 250 251
    $build['#attributes']['data-history-node-id'] = $node->id();
  }
}

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 259
 *   (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
 *   'full'.
260
 * @param string $langcode
261 262
 *   (optional) A language code to use for rendering. Defaults to the global
 *   content language of the current request.
263
 *
264
 * @return array
265
 *   An array as expected by drupal_render().
266 267 268
 *
 * @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
 *   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 281
 *   (optional) View mode; for instance, 'full', 'teaser', etc. Defaults to
 *   'full'.
282
 * @param string $langcode
283 284 285
 *   (optional) A string indicating the language field values are to be shown
 *   in. If no language is provided the current content language is used.
 *   Defaults to NULL.
286
 *
287
 * @return array
288
 *   An array in the format expected by drupal_render().
289
 *
290 291 292
 * @deprecated in Drupal 8.x and will be removed before Drupal 9.0.
 *   Use \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple().
 *
293
 * @see drupal_render()
294
 */
295 296
function comment_view_multiple($comments, $view_mode = 'full', $langcode = NULL) {
  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 304 305
  $route_match = \Drupal::routeMatch();
  if ($form_state->get('entity_type_id') == 'comment' && $route_match->getParameter('commented_entity_type')) {
    $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 319 320
  $route_match = \Drupal::routeMatch();
  if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
    $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 329 330
  $route_match = \Drupal::routeMatch();
  if ($form['#entity_type'] == 'comment' && $route_match->getParameter('commented_entity_type')) {
    $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 338
function comment_form_field_storage_config_edit_form_alter(&$form, FormStateInterface $form_state) {
  if ($form_state->getFormObject()->getEntity()->getType() == 'comment') {
339 340
    // We only support posting one comment at the time so it doesn't make sense
    // to let the site builder choose anything else.
341 342
    $form['cardinality_container']['cardinality']['#default_value'] = 1;
    $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 355
    return;
  }
356 357
  // @todo Investigate in https://www.drupal.org/node/2527866 why we need that.
  if (!\Drupal::hasService('comment.manager') || !\Drupal::service('comment.manager')->getFields($entity_type)) {
358 359 360
    // Do not query database when entity has no comment fields.
    return;
  }
361 362 363
  // Load comment information from the database and update the entity's
  // comment statistics properties, which are defined on each CommentItem field.
  $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 368
      continue;
    }
369
    $comment_statistics = $entities[$record->entity_id]->get($record->field_name);
370 371 372 373 374
    $comment_statistics->cid = $record->cid;
    $comment_statistics->last_comment_timestamp = $record->last_comment_timestamp;
    $comment_statistics->last_comment_name = $record->last_comment_name;
    $comment_statistics->last_comment_uid = $record->last_comment_uid;
    $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 384
  // maintenance of the {comment_entity_statistics} table.
  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 395 396 397 398 399
  // Entities can have non-numeric IDs, but {comment} and
  // {comment_entity_statistics} tables have integer columns for entity ID, and
  // PostgreSQL throws exceptions if you attempt query conditions with
  // mismatched types. So, we need to verify that the ID is numeric (even for an
  // entity type that has an integer ID, $entity->id() might be a string
  // containing a number), and then cast it to an integer when querying.
400
  if ($entity instanceof FieldableEntityInterface && is_numeric($entity->id())) {
401 402 403 404
    $entity_query = \Drupal::entityQuery('comment');
    $entity_query->condition('entity_id', (int) $entity->id());
    $entity_query->condition('entity_type', $entity->getEntityTypeId());
    $cids = $entity_query->execute();
405
    entity_delete_multiple('comment', $cids);
406
    \Drupal::service('comment.statistics')->delete($entity);
407
  }
408
}
409

410 411 412 413 414 415 416 417 418 419 420 421 422
/**
 * Determines if an entity type is using an integer-based ID definition.
 *
 * @param string $entity_type_id
 *   The ID the represents the entity type.
 *
 * @return bool
 *   Returns TRUE if the entity type has an integer-based ID definition and
 *   FALSE otherwise.
 */
function _comment_entity_uses_integer_id($entity_type_id) {
  $entity_type = \Drupal::entityManager()->getDefinition($entity_type_id);
  $entity_type_id_key = $entity_type->getKey('id');
423 424 425
  if ($entity_type_id_key === FALSE) {
    return FALSE;
  }
426 427 428 429 430
  $field_definitions = \Drupal::entityManager()->getBaseFieldDefinitions($entity_type->id());
  $entity_type_id_definition = $field_definitions[$entity_type_id_key];
  return $entity_type_id_definition->getType() === 'integer';
}

431
/**
432
 * Implements hook_node_update_index().
433
 */
434
function comment_node_update_index(EntityInterface $node) {
435 436 437
  $index_comments = &drupal_static(__FUNCTION__);

  if ($index_comments === NULL) {
438 439 440 441 442 443 444 445 446
    // Do not index in the following three cases:
    // 1. 'Authenticated user' can search content but can't access comments.
    // 2. 'Anonymous user' can search content but can't access comments.
    // 3. Any role can search content but can't access comments and access
    // comments is not granted by the 'authenticated user' role. In this case
    // all users might have both permissions from various roles but it is also
    // possible to set up a user to have only search content and so a user
    // edit could change the security situation so it is not safe to index the
    // 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 451
    foreach ($roles as $rid => $role) {
      if ($role->hasPermission('search content') && !$role->hasPermission('access comments')) {
452
        if ($rid == RoleInterface::AUTHENTICATED_ID || $rid == RoleInterface::ANONYMOUS_ID || !$authenticated_can_access) {
453 454 455
          $index_comments = FALSE;
          break;
        }
456 457 458 459
      }
    }
  }

460
  $build = array();
461

462
  if ($index_comments) {
463 464
    foreach (\Drupal::service('comment.manager')->getFields('node') as $field_name => $info) {
      // Skip fields that entity does not have.
465
      if (!$node->hasField($field_name)) {
466 467
        continue;
      }
468 469 470
      $field_definition = $node->getFieldDefinition($field_name);
      $mode = $field_definition->getSetting('default_mode');
      $comments_per_page = $field_definition->getSetting('per_page');
471 472 473 474 475 476
      if ($node->get($field_name)->status) {
        $comments = \Drupal::entityManager()->getStorage('comment')
          ->loadThread($node, $field_name, $mode, $comments_per_page);
        if ($comments) {
          $build[] = \Drupal::entityManager()->getViewBuilder('comment')->viewMultiple($comments);
        }
477
      }
478
    }
479
  }
480
  return \Drupal::service('renderer')->renderPlain($build);
481
}
482

483
/**
484
 * Implements hook_cron().
485
 */
486 487 488
function comment_cron() {
  // Store the maximum possible comments per thread (used for node search
  // 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 496
 *
 * Formats a comment count string and returns it, for display with search
 * results.
497
 */
498
function comment_node_search_result(EntityInterface $node) {
499 500 501 502 503
  $comment_fields = \Drupal::service('comment.manager')->getFields('node');
  $comments = 0;
  $open = FALSE;
  foreach ($comment_fields as $field_name => $info) {
    // Skip fields that entity does not have.
504
    if (!$node->hasField($field_name)) {
505 506 507 508
      continue;
    }
    // Do not make a string if comments are hidden.
    $status = $node->get($field_name)->status;
509 510
    if (\Drupal::currentUser()->hasPermission('access comments') && $status != CommentItemInterface::HIDDEN) {
      if ($status == CommentItemInterface::OPEN) {
511 512 513 514
        // At least one comment field is open.
        $open = TRUE;
      }
      $comments += $node->get($field_name)->comment_count;
515
    }
516
  }
517 518 519
  // Do not make a string if there are no comment fields, or no comments exist
  // or all comment fields are hidden.
  if ($comments > 0 || $open) {
520
    return array('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 529
  switch ($method) {
    case 'user_cancel_block_unpublish':
530
      $comments = entity_load_multiple_by_properties('comment', array('uid' => $account->id()));
531
      foreach ($comments as $comment) {
532
        $comment->setPublished(CommentInterface::NOT_PUBLISHED);
533
        $comment->save();
534
      }
535 536 537
      break;

    case 'user_cancel_reassign':
538
      /** @var \Drupal\comment\CommentInterface[] $comments */
539
      $comments = entity_load_multiple_by_properties('comment', array('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 546
      break;
  }
547 548
}

549
/**
550
 * Implements hook_ENTITY_TYPE_predelete() for user entities.
551
 */
552
function comment_user_predelete($account) {
553 554 555
  $entity_query = \Drupal::entityQuery('comment');
  $entity_query->condition('uid', $account->id());
  $cids = $entity_query->execute();
556
  entity_delete_multiple('comment', $cids);
557 558
}

559
/**
560 561
 * Generates a comment preview.
 *
562
 * @param \Drupal\comment\CommentInterface $comment
563
 *   The comment entity to preview.
564 565
 * @param Drupal\Core\Form\FormStateInterface $form_state
 *   The current state of the form.
566 567 568
 *
 * @return array
 *   An array as expected by drupal_render().
569
 */
570
function comment_preview(CommentInterface $comment, FormStateInterface $form_state) {
571
  $preview_build = array();
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 = array();
584 585
    $parent = $comment->getParentComment();
    if ($parent && $parent->isPublished()) {
586
      $build = \Drupal::entityTypeManager()->getViewBuilder('comment')->view($parent);
587
    }
588 589
  }
  else {
590 591 592 593 594 595
    // The comment field output includes rendering the parent entity of the
    // thread to which the comment is a reply. The rendered entity output
    // includes the comment reply form, which contains the comment preview and
    // therefore the rendered parent entity. This results in an infinite loop of
    // parent entity output rendering the comment form and the comment form
    // 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 601
    $entity = clone $entity;
    $entity->$field_name->status = CommentItemInterface::HIDDEN;
602
    $build = entity_view($entity, 'full');
603 604
  }

605 606
  $preview_build['comment_output_below'] = $build;
  $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 624 625 626
 * Default template: comment.html.twig.
 *
 * @param array $variables
 *   An associative array containing:
627 628
 *   - elements: An associative array containing the comment and entity objects.
 *     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 640 641 642
  $username = array(
    '#theme' => 'username',
    '#account' => $account,
  );
643
  $variables['author'] = drupal_render($username);
644
  $variables['author_id'] = $comment->getOwnerId();
645 646
  $variables['new_indicator_timestamp'] = $comment->getChangedTime();
  $variables['created'] = format_date($comment->getCreatedTime());
647
  // Avoid calling format_date() twice on the same timestamp.
648
  if ($comment->getChangedTime() == $comment->getCreatedTime()) {
649 650 651
    $variables['changed'] = $variables['created'];
  }
  else {
652
    $variables['changed'] = format_date($comment->getChangedTime());
653
  }
654

655
  if (theme_get_setting('features.comment_user_picture')) {
656 657
    // To change user picture settings (for instance, image style), edit the
    // 'compact' view mode on the User entity.
658
    $variables['user_picture'] = user_view($account, 'compact');
659 660 661 662
  }
  else {
    $variables['user_picture'] = array();
  }
663

664
  if (isset($comment->in_preview)) {
665 666
    $variables['title'] = \Drupal::l($comment->getSubject(), new Url('<front>'));
    $variables['permalink'] = \Drupal::l(t('Permalink'), new Url('<front>'));
667 668
  }
  else {
669
    $uri = $comment->permalink();
670 671 672
    $attributes = $uri->getOption('attributes') ?: array();
    $attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
    $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', array('@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 686 687 688
    $username = array(
      '#theme' => 'username',
      '#account' => $account_parent,
    );
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 694 695
      $variables['parent_changed'] = $variables['parent_created'];
    }
    else {
696
      $variables['parent_changed'] = format_date($comment_parent->getChangedTime());
697
    }
698
    $permalink_uri_parent = $comment_parent->permalink();
699 700 701
    $attributes = $permalink_uri_parent->getOption('attributes') ?: array();
    $attributes += array('class' => array('permalink'), 'rel' => 'bookmark');
    $permalink_uri_parent->setOption('attributes', $attributes);
702 703
    $variables['parent_title'] = \Drupal::l($comment_parent->getSubject(), $permalink_uri_parent);
    $variables['parent_permalink'] = \Drupal::l(t('Parent permalink'), $permalink_uri_parent);
704 705
    $variables['parent'] = t('In reply to @parent_title by @parent_username',
        array('@parent_username' => $variables['parent_author'], '@parent_title' => $variables['parent_title']));
706 707 708 709 710 711 712 713 714 715 716
  }
  else {
    $variables['parent_comment'] = '';
    $variables['parent_author'] = '';
    $variables['parent_created'] = '';
    $variables['parent_changed'] = '';
    $variables['parent_title'] = '';
    $variables['parent_permalink'] = '';
    $variables['parent'] = '';
  }

717
  // Helpful $content variable for templates.
718
  foreach (Element::children($variables['elements']) as $key) {
719 720
    $variables['content'][$key] = $variables['elements'][$key];
  }
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 740
 * @param array $variables
 *   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 745
 *
 * @todo Rename to template_preprocess_field__comment() once
 *   https://www.drupal.org/node/939462 is resolved.
746
 */
747 748 749 750 751 752 753 754
function comment_preprocess_field(&$variables) {
  $element = $variables['element'];
  if ($element['#field_type'] == 'comment') {
    // Provide contextual information.
    $variables['comment_display_mode'] = $element[0]['#comment_display_mode'];
    $variables['comment_type'] = $element[0]['#comment_type'];

    // Append additional attributes (eg. RDFa) from the first field item.
755
    $variables['attributes'] += $variables['items'][0]['attributes']->storage();
756 757 758 759 760

    // Create separate variables for the comments and comment form.
    $variables['comments'] = $element[0]['comments'];
    $variables['comment_form'] = $element[0]['comment_form'];
  }
761 762
}

763
/**
764
 * Implements hook_ranking().
765 766
 */
function comment_ranking() {
767
  return \Drupal::service('comment.statistics')->getRankingInfo();
768
}
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808

/**
 * Implements hook_ENTITY_TYPE_presave() for entity_view_display entities.
 */
function comment_entity_view_display_presave(EntityViewDisplayInterface $display) {
  // Act only on comment view displays being disabled.
  if ($display->isNew() || $display->getTargetEntityTypeId() !== 'comment' || $display->status()) {
    return;
  }
  $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  if (!$storage->loadUnchanged($display->getOriginalId())->status()) {
    return;
  }

  // Disable the comment field formatter when the used view display is disabled.
  foreach ($storage->loadMultiple() as $id => $view_display) {
    $changed = FALSE;
    /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
    foreach ($view_display->getComponents() as $field => $component) {
      if (isset($component['type']) && ($component['type'] === 'comment_default')) {
        if ($component['settings']['view_mode'] === $display->getMode()) {
          $view_display->removeComponent($field);
          /** @var \Drupal\Core\Entity\EntityViewModeInterface $mode */
          $mode = EntityViewMode::load($display->getTargetEntityTypeId() . '.' . $display->getMode());
          $arguments = [
            '@id' => $view_display->id(),
            '@name' => $field,
            '@display' => $mode->label(),
            '@mode' => $display->getMode(),
          ];
          \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);
          $changed = TRUE;
        }
      }
    }
    if ($changed) {
      $view_display->save();
    }
  }
}