function CommentSelection::getReferenceableEntities

Same name in other branches
  1. 9 core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection::getReferenceableEntities()
  2. 11.x core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php \Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection::getReferenceableEntities()

Overrides DefaultSelection::getReferenceableEntities

1 call to CommentSelection::getReferenceableEntities()
CommentSelection::countReferenceableEntities in core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php

File

core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php, line 144

Class

CommentSelection
Provides specific access control for the comment entity type.

Namespace

Drupal\comment\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $target_type = $this->getConfiguration()['target_type'];
    $query = $this->buildEntityQuery($match, $match_operator);
    if ($limit > 0) {
        $query->range(0, $limit);
    }
    $result = $query->execute();
    if (empty($result)) {
        return [];
    }
    $options = [];
    $entities = $this->entityTypeManager
        ->getStorage($target_type)
        ->loadMultiple($result);
    foreach ($entities as $entity_id => $entity) {
        // Additional access check as comments might be attached to entities
        // which the current user does not have access to.
        if ($entity->access('view', $this->currentUser)) {
            $bundle = $entity->bundle();
            $options[$bundle][$entity_id] = Html::escape($this->entityRepository
                ->getTranslationFromContext($entity)
                ->label() ?? '');
        }
    }
    return $options;
}

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