public function WorkspaceSelection::getReferenceableEntities in Drupal 9
Same name and namespace in other branches
- 8 core/modules/workspaces/src/Plugin/EntityReferenceSelection/WorkspaceSelection.php \Drupal\workspaces\Plugin\EntityReferenceSelection\WorkspaceSelection::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 DefaultSelection::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\EntityReferenceSelectionCode
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;
}