CommentedEntity.php

Same filename and directory in other branches
  1. 9 core/modules/comment/src/Plugin/views/field/CommentedEntity.php
  2. 8.9.x core/modules/comment/src/Plugin/views/field/CommentedEntity.php
  3. 10 core/modules/comment/src/Plugin/views/field/CommentedEntity.php

Namespace

Drupal\comment\Plugin\views\field

File

core/modules/comment/src/Plugin/views/field/CommentedEntity.php

View source
<?php

namespace Drupal\comment\Plugin\views\field;

use Drupal\views\Attribute\ViewsField;
use Drupal\views\Plugin\views\field\EntityField;
use Drupal\views\ResultRow;

/**
 * Views field display for 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->entityTypeManager
                    ->getStorage($type)
                    ->loadMultiple($ids);
            }
        }
        return parent::getItems($values);
    }

}

Classes

Title Deprecated Summary
CommentedEntity Views field display for commented entity.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.