Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / comment / src / Plugin / views / field / CommentedEntity.php
diff --git a/web/core/modules/comment/src/Plugin/views/field/CommentedEntity.php b/web/core/modules/comment/src/Plugin/views/field/CommentedEntity.php
new file mode 100644 (file)
index 0000000..9e0d64b
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+namespace Drupal\comment\Plugin\views\field;
+
+use Drupal\views\Plugin\views\field\EntityField;
+use Drupal\views\ResultRow;
+
+/**
+ * Views field display for commented entity.
+ *
+ * @ViewsField("commented_entity")
+ */
+class CommentedEntity extends EntityField {
+
+  /**
+   * Array of entities that has comments.
+   *
+   * We use this to load all the commented entities of same entity type at once
+   * to the EntityStorageController static cache.
+   *
+   * @var array
+   */
+  protected $loadedCommentedEntities = [];
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getItems(ResultRow $values) {
+    if (empty($this->loadedCommentedEntities)) {
+      $result = $this->view->result;
+
+      $entity_ids_per_type = [];
+      foreach ($result as $value) {
+        /** @var \Drupal\comment\CommentInterface $comment */
+        if ($comment = $this->getEntity($value)) {
+          $entity_ids_per_type[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
+        }
+      }
+
+      foreach ($entity_ids_per_type as $type => $ids) {
+        $this->loadedCommentedEntities[$type] = $this->entityManager->getStorage($type)->loadMultiple($ids);
+      }
+    }
+
+    return parent::getItems($values);
+  }
+
+}