You are here

public function ViewsUIController::autocompleteTag in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::autocompleteTag()
  2. 9 core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::autocompleteTag()

Menu callback for Views tag autocompletion.

Like other autocomplete functions, this function inspects the 'q' query parameter for the string to use to search for suggestions.

Return value

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

1 string reference to 'ViewsUIController::autocompleteTag'
views_ui.routing.yml in core/modules/views_ui/views_ui.routing.yml
core/modules/views_ui/views_ui.routing.yml

File

core/modules/views_ui/src/Controller/ViewsUIController.php, line 184

Class

ViewsUIController
Returns responses for Views UI routes.

Namespace

Drupal\views_ui\Controller

Code

public function autocompleteTag(Request $request) {
  $matches = [];
  $string = $request->query
    ->get('q');

  // Get matches from default views.
  $views = $this
    ->entityTypeManager()
    ->getStorage('view')
    ->loadMultiple();

  // Keep track of previously processed tags so they can be skipped.
  $tags = [];
  foreach ($views as $view) {
    $view_tag = $view
      ->get('tag');
    foreach (Tags::explode($view_tag) as $tag) {
      if ($tag && !in_array($tag, $tags, TRUE)) {
        $tags[] = $tag;
        if (mb_stripos($tag, $string) !== FALSE) {
          $matches[] = [
            'value' => $tag,
            'label' => Html::escape($tag),
          ];
          if (count($matches) >= 10) {
            break 2;
          }
        }
      }
    }
  }
  return new JsonResponse($matches);
}