Within the 'NO RESULTS BEHAVIOR' section of Views in Drupal 8 how do I output the search term along with some custom text using the "Unfiltered text" option?

My desired output is "No results found for search term, please..."

Can anyone suggest a solution?


you can use template_preprocess_views_view

 function template_preprocess_views_view(&$variables) {
  $view = $variables['view'];
  $variables['view_total_count'] = $view->total_rows;

  if ($view->total_rows === 0) {
    $exposed_input = $view->getExposedInput();
    $search_term = $exposed_input['your search input'];
    $variables['empty'] = 'No results found for'.$search_term.', please ...';

then in your twig template, you print the empty variable.


Maybe you could use JS.

Put a placeholder in place of "search term", maybe <span id="search-term">search term</span>, so that it still functional when JS is disabled.

Then store the search term in a variable, let's call it var searchTerm, and replace text by document.getElementById("search-term") = searchTerm.

Now all we got to do is store the search term.

  • If the search term is in URL, follow this tutorial to extract it.
  • If it isn't, please provide more detail and I'll update my solution.

Looks to be fairly old, but I will post the solution here. You can use the D8 port of search_api_spellcheck, here - https://github.com/nuvoleweb/search_api_spellcheck. It is not official release, but works well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.