function CategoryAutocompleteController::autocomplete

Same name in other branches
  1. 8.9.x core/modules/block/src/Controller/CategoryAutocompleteController.php \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()
  2. 10 core/modules/block/src/Controller/CategoryAutocompleteController.php \Drupal\block\Controller\CategoryAutocompleteController::autocomplete()
  3. 11.x 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);
}

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