1

I'm trying to display the number of total search results on my search results page, using the core search module. I've tried manipulating item-list.html.twig and item-list--search-results.html.twig by displaying items|count but when there are more than ten results, items|count only captures the number of results on that page.

I'm considering installing Search API and working with views, but that seems like overkill for this one little modification.

2
  • You will need to fetch the total results count from the pager variables. Best place to doing so is a custom implementation of the SearchController. I may provide a short example when I'm back in my office and nobody gave another answer yet. Jan 24, 2018 at 2:38
  • Added a less invasive example including theme preprocess hooks. Jan 24, 2018 at 5:15

2 Answers 2

4

The search queries of Drupal 8 use the global pager. The total results count is available within its global $pager_total_items variable.

So the easiest way of showing the amount of total items is using this variable within a hook_preprocess_HOOK() of your template in your *.theme file.

For example, if you'd like to use the total results count within the item-list--search-results.html.twig, HOOK needs to be replaced by its theme hook item_list__search_results:

/**
 * Implements hook_preprocess_HOOK() for item_list__search_results.
 */
function mytheme_preprocess_item_list__search_results(array &$variables) {
  // The $pager_total_items variable is a global
  // array keyed by the pager element increments.
  global $pager_total_items;

  // Assuming you have one paged list on your page only,
  // the element's key should be 0.
  $variables['total_items_count'] = isset($pager_total_items[0]) ? $pager_total_items[0] : 0;
}

Then you can access this variable like {{ total_items_count }} within the template.

2
  • 1
    This was perfect! The only thing missing is &$variables in the function arguments. With that addition, works like a charm. Thank you!
    – JOakland
    Jan 25, 2018 at 0:26
  • You're right. Added it accordingly. Jan 25, 2018 at 0:40
0

Using {{ total_items_count }} varible with twig template not working for me. then I fixed by overriding title of item list itself:

  • Go to /code/themes/THEMENAME
  • open THEMENAME.theme file and add following Hook

As:

function THEMENAME_preprocess_item_list__search_results(array &$variables) {
  // The $pager_total_items variable is a global
  // array keyed by the pager element increments.
  global $pager_total_items;

  // Assuming you have one paged list on your page only,
  // the element's key should be 0.
  $total_items_count = isset($pager_total_items[0]) ? $pager_total_items[0] : 0;

  $variables['title']= array(
        '#markup' =>$total_items_count .'<span class="search-count-text">results found for the search term “Search”</span>',
    );
}

**Please change THEMENAME with you theme name

1
  • It's not good practice to modify Drupal core files. Your modifications will be lost after each Drupal core updates. You are better creating a custom theme which extends the theme you are using, and do your modifications in there. File: /themes/custom/THEMENAME/THEMENAME.info.yml, type: theme, base theme: classy, ... Change THEMENAME for a new name specific for your website Jan 12, 2021 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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