You are here

public function TaxonomyHierarchySelection::getReferenceableEntities in Workbench Access 8

Gets the list of referenceable entities.

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

1 call to TaxonomyHierarchySelection::getReferenceableEntities()
TaxonomyHierarchySelection::validateReferenceableEntities in src/Plugin/EntityReferenceSelection/TaxonomyHierarchySelection.php
Validates which existing entities can be referenced.

File

src/Plugin/EntityReferenceSelection/TaxonomyHierarchySelection.php, line 105

Class

TaxonomyHierarchySelection
Provides specific access control for the taxonomy_term entity type.

Namespace

Drupal\workbench_access\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {

  // Get the base options list from the normal handler. We will filter later.
  if ($match || $limit) {
    $options = parent::getReferenceableEntities($match, $match_operator, $limit);
  }
  else {
    $options = [];
    $bundles = $this->entityTypeBundleInfo
      ->getBundleInfo('taxonomy_term');
    if (isset($this->configuration['handler_settings'])) {
      $handler_settings = $this->configuration['handler_settings'];
    }
    $bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : array_keys($bundles);
    foreach ($bundle_names as $bundle) {
      if ($vocabulary = $this->entityTypeManager
        ->getStorage('taxonomy_vocabulary')
        ->load($bundle)) {
        if ($terms = $this->entityTypeManager
          ->getStorage('taxonomy_term')
          ->loadTree($vocabulary
          ->id(), 0, NULL, TRUE)) {
          foreach ($terms as $term) {
            $options[$vocabulary
              ->id()][$term
              ->id()] = str_repeat('-', $term->depth) . Html::escape($this->entityRepository
              ->getTranslationFromContext($term)
              ->label());
          }
        }
      }
    }
  }

  // Now, filter the options by permission.
  // If assigned to the top level or a superuser, no alteration.
  if ($this->currentUser
    ->hasPermission('bypass workbench access')) {
    return $options;
  }

  // Check each section for access.
  $user_sections = $this->userSectionStorage
    ->getUserSections($this->scheme);
  foreach ($options as $key => $values) {
    if (WorkbenchAccessManager::checkTree($this->scheme, [
      $key,
    ], $user_sections)) {
      continue;
    }
    else {
      foreach ($values as $id => $value) {
        if (!WorkbenchAccessManager::checkTree($this->scheme, [
          $id,
        ], $user_sections)) {
          unset($options[$key][$id]);
        }
      }
    }
  }
  return $options;
}