function TermSelection::getReferenceableEntities

Same name in other branches
  1. 9 core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php \Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection::getReferenceableEntities()
  2. 8.9.x core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php \Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection::getReferenceableEntities()
  3. 10 core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php \Drupal\taxonomy\Plugin\EntityReferenceSelection\TermSelection::getReferenceableEntities()

Overrides DefaultSelection::getReferenceableEntities

1 call to TermSelection::getReferenceableEntities()
TermSelection::countReferenceableEntities in core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php
Counts entities that are referenceable.

File

core/modules/taxonomy/src/Plugin/EntityReferenceSelection/TermSelection.php, line 53

Class

TermSelection
Provides specific access control for the taxonomy_term entity type.

Namespace

Drupal\taxonomy\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    if ($match || $limit) {
        return parent::getReferenceableEntities($match, $match_operator, $limit);
    }
    $options = [];
    $bundles = $this->entityTypeBundleInfo
        ->getBundleInfo('taxonomy_term');
    $bundle_names = $this->getConfiguration()['target_bundles'] ?: array_keys($bundles);
    $has_admin_access = $this->currentUser
        ->hasPermission('administer taxonomy');
    $unpublished_terms = [];
    foreach ($bundle_names as $bundle) {
        if ($vocabulary = Vocabulary::load($bundle)) {
            
            /** @var \Drupal\taxonomy\TermInterface[] $terms */
            if ($terms = $this->entityTypeManager
                ->getStorage('taxonomy_term')
                ->loadTree($vocabulary->id(), 0, NULL, TRUE)) {
                foreach ($terms as $term) {
                    if (!$has_admin_access && (!$term->isPublished() || in_array($term->parent->target_id, $unpublished_terms))) {
                        $unpublished_terms[] = $term->id();
                        continue;
                    }
                    $options[$vocabulary->id()][$term->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityRepository
                        ->getTranslationFromContext($term)
                        ->label());
                }
            }
        }
    }
    return $options;
}

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