function comment_entity_storage_load in Drupal 9
Same name and namespace in other branches
- 8 core/modules/comment/comment.module \comment_entity_storage_load()
 
Implements hook_entity_storage_load().
See also
\Drupal\comment\Plugin\Field\FieldType\CommentItem::propertyDefinitions()
File
- core/
modules/ comment/ comment.module, line 272  - Enables users to comment on published content.
 
Code
function comment_entity_storage_load($entities, $entity_type) {
  // 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;
  }
}