function EntityLinkSuggestionsController::getSuggestions
Gets the suggestions.
Parameters
string $target_entity_type_id: An entity type to get suggestions for.
null|string[] $target_bundles: NULL to allow all bundles, a list of bundle names to restrict to those bundles.
string $string: The string to search.
string $host_entity_langcode: The langcode of the host entity.
Return value
array An array of suggestion objects with populated entity data.
See also
\Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::defaultConfiguration()
File
-
core/
modules/ ckeditor5/ src/ Controller/ EntityLinkSuggestionsController.php, line 170
Class
- EntityLinkSuggestionsController
- Returns responses for entity link suggestions autocomplete route.
Namespace
Drupal\ckeditor5\ControllerCode
public function getSuggestions(string $target_entity_type_id, ?array $target_bundles, string $string, string $host_entity_langcode) : array {
// If the user input is a current entity URL, don't get more suggestions.
if ($entity_id = static::findEntityIdByUrl($target_entity_type_id, $string)) {
$entity = $this->entityTypeManager()
->getStorage($target_entity_type_id)
->load($entity_id);
if ($entity?->language()->getId() === $host_entity_langcode) {
return [
$this->createSuggestion($entity),
];
}
}
// Do not call ::getPluginId() or ::getInstance() because this favors a
// "link_target" variant of the default selection plugin for the given
// entity type, if it exists.
$selection_handler_groups = $this->selectionPluginManager
->getSelectionGroups($target_entity_type_id);
if (!array_key_exists('default', $selection_handler_groups)) {
return [];
}
// Sort the selection plugins by weight and select the best match.
uasort($selection_handler_groups['default'], [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
end($selection_handler_groups['default']);
// Select the link_target variant of the default selection plugin for the
// entity type, if it exists. Otherwise, select the next best match.
$link_target_selection_plugin_id = "default:{$target_entity_type_id}_link_target";
$plugin_id = array_key_exists($link_target_selection_plugin_id, $selection_handler_groups['default']) ? $link_target_selection_plugin_id : key($selection_handler_groups['default']);
$selection = $this->selectionPluginManager
->createInstance($plugin_id, [
'target_type' => $target_entity_type_id,
'target_bundles' => $target_bundles,
]);
$entities_by_bundle = $selection->getReferenceableEntities($string, 'CONTAINS', static::DEFAULT_LIMIT);
// DefaultSelection::getReferenceableEntities() loads entities and even
// their translation but then only keeps bundle, entity ID and label. Reload
// them to generate rich results. Note that performance overhead of this is
// minimal because all this data is statically cached already anyway.
$entity_ids = array_reduce($entities_by_bundle, function ($flattened, $bundle_entities) {
return array_merge($flattened, array_keys($bundle_entities));
}, []);
$entities = $this->entityTypeManager()
->getStorage($target_entity_type_id)
->loadMultiple($entity_ids);
$suggestions = [];
foreach ($entities as $entity) {
$entity_translation = $entity->getEntityType()
->isTranslatable() && $entity->hasTranslation($host_entity_langcode) ? $entity->getTranslation($host_entity_langcode) : $entity;
if ($entity_translation->language()
->getId() === $host_entity_langcode) {
$suggestions[] = $this->createSuggestion($entity_translation);
}
}
return $suggestions;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.