I want to use an autocomplete input field for the global search on my Drupal 9 page. But instead of listing term suggestions to show what the user might search I use a custom controller and a search api view to find and display the matching result nodes as "autocomplete" results. So in the step after getting the results from my search api view by
$view = Views::getView('search_content');
if (is_object($view)) {
$view->setArguments($args);
$view->setDisplay('autocomplete');
$view->execute();
}
I want to return all these results to the JavaScript function of the tomickigrzegorz/autocomplete
module. I have to place every result into my array $matches['label'
and return it as a JSON object. The receiving JavaScript function will map every element and place it into a <li>
element.
Until here my code works properly.
My question is: Can I render my view result rows (using the appropriate view mode defined in the view row settings) in my php controller like I can render the whole view by doing
$rendered = $view->render();
$output = \Drupal::service('renderer')->render($rendered);
I found serveral tips but could only find some for Drupal 7.