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.