You are here

public function ContextUIController::groupsAutocomplete in Context 8

Same name and namespace in other branches
  1. 8.4 modules/context_ui/src/Controller/ContextUIController.php \Drupal\context_ui\Controller\ContextUIController::groupsAutocomplete()
  2. 8.0 modules/context_ui/src/Controller/ContextUIController.php \Drupal\context_ui\Controller\ContextUIController::groupsAutocomplete()

Retrieves group suggestions for a context.

Parameters

Request $request: The current request.

Return value

JsonResponse A JSON response with groups matching the query.

1 string reference to 'ContextUIController::groupsAutocomplete'
context_ui.routing.yml in modules/context_ui/context_ui.routing.yml
modules/context_ui/context_ui.routing.yml

File

modules/context_ui/src/Controller/ContextUIController.php, line 88

Class

ContextUIController

Namespace

Drupal\context_ui\Controller

Code

public function groupsAutocomplete(Request $request) {
  $query = $request->query
    ->get('q');
  $matches = [];
  foreach ($this->contextManager
    ->getContexts() as $context) {
    if (stripos($context
      ->getGroup(), $query) === 0) {
      $matches[] = $context
        ->getGroup();
    }
  }
  $response = [];

  // Format the unique matches to be used with the autocomplete field.
  foreach (array_unique($matches) as $match) {
    $response[] = [
      'value' => $match,
      'label' => Html::escape($match),
    ];
  }
  return new JsonResponse($response);
}