You are here

public function AutocompleteController::autocomplete in Linkit 8.5

Same name and namespace in other branches
  1. 8.4 src/Controller/AutocompleteController.php \Drupal\linkit\Controller\AutocompleteController::autocomplete()

Menu callback for linkit search autocompletion.

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.

string $linkit_profile_id: The linkit profile id.

Return value

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

1 string reference to 'AutocompleteController::autocomplete'
linkit.routing.yml in ./linkit.routing.yml
linkit.routing.yml

File

src/Controller/AutocompleteController.php, line 75

Class

AutocompleteController
Returns responses for linkit autocomplete routes.

Namespace

Drupal\linkit\Controller

Code

public function autocomplete(Request $request, $linkit_profile_id) {
  $this->linkitProfile = $this->linkitProfileStorage
    ->load($linkit_profile_id);
  $string = $request->query
    ->get('q');
  $suggestionCollection = $this->suggestionManager
    ->getSuggestions($this->linkitProfile, mb_strtolower($string));

  /*
   * If there are no suggestions from the matcher plugins, we have to add a
   * special suggestion that have the same path as the given string so users
   * can select it and use it anyway. This is a common use case with external
   * links.
   */
  if (!count($suggestionCollection
    ->getSuggestions()) && !empty($string)) {
    $suggestionCollection = $this->suggestionManager
      ->addUnscathedSuggestion($suggestionCollection, $string);
  }
  return new JsonResponse($suggestionCollection);
}