Views - Inserting search term within "No results behavior"

by cwiggo   Last Updated January 12, 2018 16:07 PM

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?

Thanks in advance

Answers 2

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.
Chris Happy
Chris Happy
December 06, 2016 01:08 AM

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.

Mohammed ATIFI
Mohammed ATIFI
January 12, 2018 15:12 PM

Related Questions

Drupal default search not working

Updated June 11, 2015 06:03 AM

views exposed filter listen to node form values

Updated March 26, 2015 07:24 AM