I'm trying to add a Twig template suggestion for list pages via pagers, so that I can theme the first page differently than the next pages.

I have been able to successfully add a template suggestion with the following code.

function MYTHEME_theme_suggestions_page_alter(array &$suggestions, array $variables) {

  $path = \Drupal::request()->getRequestUri();
  $strip_path = parse_url($path, PHP_URL_QUERY);

  $new_path = str_replace("page=", "pager", $strip_path);
  $new_path = preg_replace('/[0-9]+/', '', $new_path);

  if (!empty($new_path)) {
    $suggestions[] = 'page__' . $new_path;

At the moment I'm getting page--pager.html.twig as a suggestion on the pager pages only, not the first page of the list, (it is desired).

If Drupal URL is /something?page=[some-number] the above suggestion is provided, however, if it is just /something template suggestion is not suggested.

In Devel profiler, I have not seen any extra overhead or memory usage.

Is it the right way, or is there any better way?

  • Thing is that this snippet now fires on every single page, doesn't it? Not just on pagers, and not just on the first pager page. Can you confirm? – leymannx Jul 11 '18 at 11:54
  • 1
    Yes, at the moment, it is being fired on every single page, not just the pagers pages. – Troy Anderson Jul 11 '18 at 12:02
  • I updated the title to clarify :) – leymannx Jul 11 '18 at 12:21
  • 1
    Thank you, however, I do not get a template suggestion on any other page, other than a page with query string ?page > 0. Hope that clarifies it further. – Troy Anderson Jul 11 '18 at 12:23
  • Absolutely! Maybe you can update your question to add both comments directly into your question. Which will help others to quickly get what's really important. – leymannx Jul 11 '18 at 12:34

Your Answer

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

Browse other questions tagged or ask your own question.