You are here

public function WorkspaceSelection::getReferenceableEntities in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php \Drupal\workspaces\Plugin\EntityReferenceSelection\WorkspaceSelection::getReferenceableEntities()
  2. 9 core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php \Drupal\workspaces\Plugin\EntityReferenceSelection\WorkspaceSelection::getReferenceableEntities()

File

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

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;
}