8.5.x comment.module comment_node_update_index(EntityInterface $node)
8.0.x comment.module comment_node_update_index(EntityInterface $node)
8.1.x comment.module comment_node_update_index(EntityInterface $node)
8.2.x comment.module comment_node_update_index(EntityInterface $node)
8.3.x comment.module comment_node_update_index(EntityInterface $node)
8.4.x comment.module comment_node_update_index(EntityInterface $node)
8.6.x comment.module comment_node_update_index(EntityInterface $node)
7.x comment.module comment_node_update_index($node)

Implements hook_node_update_index().

File

core/modules/comment/comment.module, line 425
Enables users to comment on published content.

Code

function comment_node_update_index(EntityInterface $node) {
  $index_comments =& drupal_static(__FUNCTION__);
  if ($index_comments === NULL) {

    // 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.
    $index_comments = TRUE;
    $roles = \Drupal::entityManager()
      ->getStorage('user_role')
      ->loadMultiple();
    $authenticated_can_access = $roles[RoleInterface::AUTHENTICATED_ID]
      ->hasPermission('access comments');
    foreach ($roles as $rid => $role) {
      if ($role
        ->hasPermission('search content') && !$role
        ->hasPermission('access comments')) {
        if ($rid == RoleInterface::AUTHENTICATED_ID || $rid == RoleInterface::ANONYMOUS_ID || !$authenticated_can_access) {
          $index_comments = FALSE;
          break;
        }
      }
    }
  }
  $build = array();
  if ($index_comments) {
    foreach (\Drupal::service('comment.manager')
      ->getFields('node') as $field_name => $info) {

      // Skip fields that entity does not have.
      if (!$node
        ->hasField($field_name)) {
        continue;
      }
      $field_definition = $node
        ->getFieldDefinition($field_name);
      $mode = $field_definition
        ->getSetting('default_mode');
      $comments_per_page = $field_definition
        ->getSetting('per_page');
      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);
        }
      }
    }
  }
  return \Drupal::service('renderer')
    ->renderPlain($build);
}