You are here

public function MenuSelectAutocompleteController::autocomplete in Menu Select 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/MenuSelectAutocompleteController.php \Drupal\menu_select\Controller\MenuSelectAutocompleteController::autocomplete()

Returns autocomplete content for the given search queries.

Return value

\Symfony\Component\HttpFoundation\JsonResponse A JSON response.

1 string reference to 'MenuSelectAutocompleteController::autocomplete'
menu_select.routing.yml in ./menu_select.routing.yml
menu_select.routing.yml

File

src/Controller/MenuSelectAutocompleteController.php, line 38

Class

MenuSelectAutocompleteController
Defines a controller class with methods for auto complete.

Namespace

Drupal\menu_select\Controller

Code

public function autocomplete($menus, $max_depth, Request $request) {
  $keyword = $request->query
    ->get('q');
  $menu_ids = explode(':', $menus);
  $matching_links = $this
    ->getMatchingLinks($keyword, $menu_ids, $max_depth);
  $autocomplete = [];
  foreach ($matching_links as $key => $label) {
    $autocomplete[] = [
      'value' => (string) $key,
      'label' => $label,
    ];
  }
  return new JsonResponse($autocomplete);
}