You are here

function comment_entity_storage_load in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 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 339
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::entityManager()
    ->getDefinition($entity_type)
    ->isSubclassOf('Drupal\\Core\\Entity\\FieldableEntityInterface')) {
    return;
  }

  // @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)) {

    // 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;
  }
}