9e0d64b23e000ab2378fff31e51d96f860408ee3
[yaffs-website] / web / core / modules / comment / src / Plugin / views / field / CommentedEntity.php
1 <?php
2
3 namespace Drupal\comment\Plugin\views\field;
4
5 use Drupal\views\Plugin\views\field\EntityField;
6 use Drupal\views\ResultRow;
7
8 /**
9  * Views field display for commented entity.
10  *
11  * @ViewsField("commented_entity")
12  */
13 class CommentedEntity extends EntityField {
14
15   /**
16    * Array of entities that has comments.
17    *
18    * We use this to load all the commented entities of same entity type at once
19    * to the EntityStorageController static cache.
20    *
21    * @var array
22    */
23   protected $loadedCommentedEntities = [];
24
25   /**
26    * {@inheritdoc}
27    */
28   public function getItems(ResultRow $values) {
29     if (empty($this->loadedCommentedEntities)) {
30       $result = $this->view->result;
31
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();
37         }
38       }
39
40       foreach ($entity_ids_per_type as $type => $ids) {
41         $this->loadedCommentedEntities[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);
42       }
43     }
44
45     return parent::getItems($values);
46   }
47
48 }