Pager.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Drupal\Core\Render\Element;
  3. /**
  4. * Provides a render element for a pager.
  5. *
  6. * The pager must be initialized with a call to pager_default_initialize() in
  7. * order to render properly. When used with database queries, this is performed
  8. * for you when you extend a select query with
  9. * \Drupal\Core\Database\Query\PagerSelectExtender.
  10. *
  11. * Properties:
  12. * - #element: (optional, int) The pager ID, to distinguish between multiple
  13. * pagers on the same page (defaults to 0).
  14. * - #parameters: (optional) An associative array of query string parameters to
  15. * append to the pager.
  16. * - #quantity: The maximum number of numbered page links to create (defaults
  17. * to 9).
  18. * - #tags: (optional) An array of labels for the controls in the pages.
  19. * - #route_name: (optional) The name of the route to be used to build pager
  20. * links. Defaults to '<none>', which will make links relative to the current
  21. * URL. This makes the page more effectively cacheable.
  22. *
  23. * @code
  24. * $build['pager'] = [
  25. * '#type' => 'pager',
  26. * ];
  27. * @endcode
  28. *
  29. * @RenderElement("pager")
  30. */
  31. class Pager extends RenderElement {
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getInfo() {
  36. return [
  37. '#pre_render' => [
  38. get_class($this) . '::preRenderPager',
  39. ],
  40. '#theme' => 'pager',
  41. // The pager ID, to distinguish between multiple pagers on the same page.
  42. '#element' => 0,
  43. // An associative array of query string parameters to append to the pager
  44. // links.
  45. '#parameters' => [],
  46. // The number of pages in the list.
  47. '#quantity' => 9,
  48. // An array of labels for the controls in the pager.
  49. '#tags' => [],
  50. // The name of the route to be used to build pager links. By default no
  51. // path is provided, which will make links relative to the current URL.
  52. // This makes the page more effectively cacheable.
  53. '#route_name' => '<none>',
  54. ];
  55. }
  56. /**
  57. * #pre_render callback to associate the appropriate cache context.
  58. *
  59. *
  60. * @param array $pager
  61. * A renderable array of #type => pager.
  62. *
  63. * @return array
  64. */
  65. public static function preRenderPager(array $pager) {
  66. // Note: the default pager theme process function
  67. // template_preprocess_pager() also calls pager_query_add_page(), which
  68. // maintains the existing query string. Therefore
  69. // template_preprocess_pager() adds the 'url.query_args' cache context,
  70. // which causes the more specific cache context below to be optimized away.
  71. // In other themes, however, that may not be the case.
  72. $pager['#cache']['contexts'][] = 'url.query_args.pagers:' . $pager['#element'];
  73. return $pager;
  74. }
  75. }