PathParentCacheContext.php 885 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. /**
  5. * Defines a cache context service for path parents.
  6. *
  7. * Cache context ID: 'url.path.parent'.
  8. *
  9. * This allows for caching based on the path, excluding everything after the
  10. * last forward slash.
  11. */
  12. class PathParentCacheContext extends RequestStackCacheContextBase implements CacheContextInterface {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public static function getLabel() {
  17. return t('Parent path');
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function getContext() {
  23. $request = $this->requestStack->getCurrentRequest();
  24. $path_elements = explode('/', trim($request->getPathInfo(), '/'));
  25. array_pop($path_elements);
  26. return implode('/', $path_elements);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function getCacheableMetadata() {
  32. return new CacheableMetadata();
  33. }
  34. }