I updated a website to Drupal 8.9.6 version. I saw that the global variable $pager_total_items was used, but is deprecated since D8.8.0. So, I'm looking for an alternative to retrieve the total items of the pager. This was used on a Search API page.

The old code looks like this :

$variables['count_results']     = empty($GLOBALS['pager_total_items']) ? 0 : $GLOBALS['pager_total_items'][0];

And now, looking this change record : https://www.drupal.org/node/2779457 I try to replace this like this :

/** @var \Drupal\Core\Pager\PagerManager $pagerManager */
$pagerManager = \Drupal::service('pager.manager');

This renders nothing in the dpm(), so no Pager was found. It appears that Search API doesn't use a Pager...


This works for me:

$pager = \Drupal::service('pager.manager')->getPager();
$count_results = $pager->getTotalItems();

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.