function ViewsUIController::autocompleteTag

Same name in other branches
  1. 9 core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::autocompleteTag()
  2. 8.9.x core/modules/views_ui/src/Controller/ViewsUIController.php \Drupal\views_ui\Controller\ViewsUIController::autocompleteTag()
  3. 10 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 174

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);
}

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