You are here

public function DirectorySelection::getReferenceableEntities in Media Directories 2.x

Same name and namespace in other branches
  1. 8 src/Plugin/EntityReferenceSelection/DirectorySelection.php \Drupal\media_directories\Plugin\EntityReferenceSelection\DirectorySelection::getReferenceableEntities()
  2. 3.x src/Plugin/EntityReferenceSelection/DirectorySelection.php \Drupal\media_directories\Plugin\EntityReferenceSelection\DirectorySelection::getReferenceableEntities()

Gets the list of referenceable entities.

Parameters

string|null $match: (optional) Text to match the label against. Defaults to NULL.

string $match_operator: (optional) Operator to be used for string matching. Defaults to "CONTAINS".

int $limit: (optional) Limit the query to a given number of items. Defaults to 0, which indicates no limiting.

Return value

array A nested array of entities, the first level is keyed by the entity bundle, which contains an array of entity labels (escaped), keyed by the entity ID.

Overrides TermSelection::getReferenceableEntities

File

src/Plugin/EntityReferenceSelection/DirectorySelection.php, line 33

Class

DirectorySelection
Default plugin implementation of the Entity Reference Selection plugin.

Namespace

Drupal\media_directories\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;
          }

          // Change default padding character and add an extra one.
          $options[$vocabulary
            ->id()][$term
            ->id()] = str_repeat('−', $term->depth + 1) . Html::escape($this->entityRepository
            ->getTranslationFromContext($term)
            ->label());
        }
      }
    }
  }
  return $options;
}