function EntityLinkSuggestionsController::suggestions

Generates entity link suggestions for use by an autocomplete.

Like other autocomplete functions, this function inspects the 'q' query parameter for the string to use to search for suggestions.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request.

\Drupal\editor\EditorInterface $editor: The text editor whose drupalEntityLinkSuggestions configuration to use.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response containing the autocomplete suggestions.

1 string reference to 'EntityLinkSuggestionsController::suggestions'
ckeditor5.routing.yml in core/modules/ckeditor5/ckeditor5.routing.yml
core/modules/ckeditor5/ckeditor5.routing.yml

File

core/modules/ckeditor5/src/Controller/EntityLinkSuggestionsController.php, line 82

Class

EntityLinkSuggestionsController
Returns responses for entity link suggestions autocomplete route.

Namespace

Drupal\ckeditor5\Controller

Code

public function suggestions(Request $request, EditorInterface $editor) : JsonResponse {
  $input = mb_strtolower($request->query
    ->get('q'));
  $host_entity_type_id = $request->query
    ->get('hostEntityTypeId');
  $host_entity_langcode = $request->query
    ->get('hostEntityLangcode');
  $suggestions = [];
  if ($input) {
    $allowed_bundles = [];
    $all_bundle_info = $this->entityTypeBundleInfo
      ->getAllBundleInfo();
    foreach ($all_bundle_info as $entity_type => $bundles) {
      foreach ($bundles as $key => $bundle) {
        if (!empty($bundle['ckeditor5_link_suggestions'])) {
          $allowed_bundles[$entity_type][$key] = $key;
        }
      }
    }
    if (in_array($host_entity_type_id, array_keys($allowed_bundles), TRUE)) {
      $suggestions = $this->getSuggestions($host_entity_type_id, $allowed_bundles[$host_entity_type_id], $input, $host_entity_langcode);
    }
    // Second, find suggestions for all other entity types, in the specified
    // order.
    $allowed_entity_type_ids = array_keys($allowed_bundles);
    foreach ($allowed_bundles as $entity_type_id => $bundles) {
      if ($host_entity_type_id === $entity_type_id) {
        continue;
      }
      if (in_array($entity_type_id, $allowed_entity_type_ids, TRUE)) {
        $suggestions = array_merge($suggestions, $this->getSuggestions($entity_type_id, $bundles, $input, $host_entity_langcode));
      }
    }
    // If no suggestions were found, add a special suggestion that has the
    // same path as the given string so users can select it and use it anyway.
    // This typically occurs when entering external links.
    if (!$suggestions) {
      $suggestions = [
        [
          'description' => $this->t('No content suggestions found. This URL will be used as is.'),
          'group' => $this->t('No results'),
          'label' => Html::escape($input),
          'href' => UrlHelper::isValid($input) ? $input : '',
        ],
      ];
    }
  }
  // Note that we intentionally:
  // - do not use \Drupal\Core\Cache\CacheableJsonResponse because caching it
  //   on the server side is wasteful, hence there is no need for cacheability
  //   metadata.
  // - mark the response as private, because the suggestions include only the
  //   ones accessible by the current user.
  return (new JsonResponse([
    'suggestions' => $suggestions,
  ]))->setPrivate()
    ->setMaxAge(300);
}

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