PagersCacheContext.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. /**
  5. * Defines a cache context for "per page in a pager" caching.
  6. *
  7. * Cache context ID: 'url.query_args.pagers' (to vary by all pagers).
  8. * Calculated cache context ID: 'url.query_args.pagers:%pager_id', e.g.
  9. * 'url.query_args.pagers:1' (to vary by the pager with ID 1).
  10. */
  11. class PagersCacheContext extends RequestStackCacheContextBase implements CalculatedCacheContextInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public static function getLabel() {
  16. return t('Pager');
  17. }
  18. /**
  19. * {@inheritdoc}
  20. *
  21. * @see pager_find_page()
  22. */
  23. public function getContext($pager_id = NULL) {
  24. // The value of the 'page' query argument contains the information that
  25. // controls *all* pagers.
  26. if ($pager_id === NULL) {
  27. return $this->requestStack->getCurrentRequest()->query->get('page', '');
  28. }
  29. return $pager_id . '.' . pager_find_page($pager_id);
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function getCacheableMetadata($pager_id = NULL) {
  35. return new CacheableMetadata();
  36. }
  37. }