3 namespace Drupal\comment\Plugin\views\field;
5 use Drupal\views\Plugin\views\field\EntityField;
6 use Drupal\views\ResultRow;
9 * Views field display for commented entity.
11 * @ViewsField("commented_entity")
13 class CommentedEntity extends EntityField {
16 * Array of entities that has comments.
18 * We use this to load all the commented entities of same entity type at once
19 * to the EntityStorageController static cache.
23 protected $loadedCommentedEntities = [];
28 public function getItems(ResultRow $values) {
29 if (empty($this->loadedCommentedEntities)) {
30 $result = $this->view->result;
32 $entity_ids_per_type = [];
33 foreach ($result as $value) {
34 /** @var \Drupal\comment\CommentInterface $comment */
35 if ($comment = $this->getEntity($value)) {
36 $entity_ids_per_type[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
40 foreach ($entity_ids_per_type as $type => $ids) {
41 $this->loadedCommentedEntities[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);
45 return parent::getItems($values);