function CommentHooks::entityStorageLoad

Implements hook_entity_storage_load().

Attributes

#[Hook('entity_storage_load')]

See also

\Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()

File

core/modules/comment/src/Hook/CommentHooks.php, line 240

Class

CommentHooks
Hook implementations for comment.

Namespace

Drupal\comment\Hook

Code

public function entityStorageLoad($entities, $entity_type) : void {
  // Comments can only be attached to content entities, so skip others.
  if (!\Drupal::entityTypeManager()->getDefinition($entity_type)
    ->entityClassImplements(FieldableEntityInterface::class)) {
    return;
  }
  if (!\Drupal::service('comment.manager')->getFields($entity_type)) {
    // Do not query database when entity has no comment fields.
    return;
  }
  // 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);
  foreach ($result as $record) {
    // Skip fields that entity does not have.
    if (!$entities[$record->entity_id]
      ->hasField($record->field_name)) {
      continue;
    }
    $comment_statistics = $entities[$record->entity_id]
      ->get($record->field_name);
    $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;
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.