function WorkspaceSelection::getReferenceableEntities

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

Overrides DefaultSelection::getReferenceableEntities

File

core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php, line 69

Class

WorkspaceSelection
Provides specific access control for the workspace entity type.

Namespace

Drupal\workspaces\Plugin\EntityReferenceSelection

Code

public function getReferenceableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    // Get all the workspace entities and sort them in tree order.
    $storage = $this->entityTypeManager
        ->getStorage('workspace');
    $workspace_tree = $this->workspaceRepository
        ->loadTree();
    $entities = array_replace($workspace_tree, $storage->loadMultiple());
    // If we need to restrict the list of workspaces by searching only a part of
    // their label ($match) or by a number of results ($limit), the workspace
    // tree would be mangled because it wouldn't contain all the tree items.
    if ($match || $limit) {
        $options = parent::getReferenceableEntities($match, $match_operator, $limit);
    }
    else {
        $options = [];
        foreach ($entities as $entity) {
            $options[$entity->bundle()][$entity->id()] = str_repeat('-', $workspace_tree[$entity->id()]['depth']) . Html::escape($this->entityRepository
                ->getTranslationFromContext($entity)
                ->label());
        }
    }
    $restricted_access_entities = [];
    foreach ($options as $bundle => $bundle_options) {
        foreach (array_keys($bundle_options) as $id) {
            // If a user can not view a workspace, we need to prevent them from
            // referencing that workspace as well as its descendants.
            if (in_array($id, $restricted_access_entities) || !$entities[$id]->access('view', $this->currentUser)) {
                $restricted_access_entities += $workspace_tree[$id]['descendants'];
                unset($options[$bundle][$id]);
            }
        }
    }
    return $options;
}

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