Highlight search in an exposed view


I have created a view in which you can search a keyword with an exposed filter.
Is there a way that the keyword, that the user has typed in, could be highlighted?


Hi Nadine,

I searched a bit about that but there is not an easy way that does this out of the box.

Hi Sotirios,

What a pity! Do you have a link to a good page where it describes the possibility of doing that? Just for interest :wink:

Sure. You need to download and install the CERN override theme.

Then you need to add a template to preprocess the search results (https://api.drupal.org/api/drupal/core!modules!search!search.pages.inc/function/template_preprocess_search_result/8.8.x).

Within the template you need to get the value of the keyword that you are searching for using \Drupal::request()->query->get(‘keys’)

Once you get the keyword you need to search in the results values for this keyword and use the search_excerpt function that highlights it within the text.

$keyword = \Drupal::request()->query->get(‘keys’);
search_excerpt($keyword, $results);

I am sorry but I can’t provide more help or dig into that. You should only try to do that if you know PHP programming and Drupal’s Search API well. That’s only an example and not the full solution.

Thank you! That’s really interesting.