0

I've added a pager to a custom page rendered programmatically from a controller. I want to customize the pager. How can I do it without overriding the standard pager twig?

This is the hook_theme:

function module_theme()
{
     return [
          'custom_page' => ['variables' => ['data' => null, 'pager' => null]]
     ];
}

This is the render array in the controller:

public function customPage()
{
     return [
          '#theme' => 'custom_page',
          '#data' => $data,
          '#pager' => [
               '#type' => 'pager',
          ],
     ];
}

The template showing on 'core/modules/system/templates/pager.html.twig'.

How can I change the template for another custom template?

1 Answer 1

2

I think you have 2 possibilities:

1.) Copy the original pager.html.twig in your theme folder and adjust the template for your needs. This will adjust every pager whenever this theme is active.

2.) Define your own template suggestion for your pager:

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function your_module_theme_suggestions_pager_alter(array &$suggestions, array $variables) {
   ... 
   here you do some magic like "when in the $variables array is something specific
   $suggestions[] = 'pager__your_template'
   ...
}

Then you can use your own template suggestion (in this case pager__your_template.html.twig) in this specific case. Hopefully this already helps you.

1
  • thanks, it worked :) Commented Aug 30, 2022 at 21:16

Your Answer

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

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