You are here

public function CategoryAutocompleteController::autocomplete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/block/src/Controller/CategoryAutocompleteController.php \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()

Retrieves suggestions for block category autocompletion.

Parameters

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

Return value

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

1 string reference to 'CategoryAutocompleteController::autocomplete'
block.routing.yml in core/modules/block/block.routing.yml
core/modules/block/block.routing.yml

File

core/modules/block/src/Controller/CategoryAutocompleteController.php, line 52

Class

CategoryAutocompleteController
Returns autocomplete responses for block categories.

Namespace

Drupal\block\Controller

Code

public function autocomplete(Request $request) {
  $typed_category = $request->query
    ->get('q');
  $matches = [];
  foreach ($this->blockManager
    ->getCategories() as $category) {
    if (stripos($category, $typed_category) === 0) {
      $matches[] = [
        'value' => $category,
        'label' => Html::escape($category),
      ];
    }
  }
  return new JsonResponse($matches);
}