IsFrontPathCacheContext.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Path\PathMatcherInterface;
  5. /**
  6. * Defines a cache context for whether the URL is the front page of the site.
  7. *
  8. * Cache context ID: 'url.path.is_front'.
  9. */
  10. class IsFrontPathCacheContext implements CacheContextInterface {
  11. /**
  12. * @var \Drupal\Core\Path\PathMatcherInterface
  13. */
  14. protected $pathMatcher;
  15. /**
  16. * Constructs an IsFrontPathCacheContext object.
  17. *
  18. * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
  19. */
  20. public function __construct(PathMatcherInterface $path_matcher) {
  21. $this->pathMatcher = $path_matcher;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static function getLabel() {
  27. return t('Is front page');
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function getContext() {
  33. return 'is_front.' . (int) $this->pathMatcher->isFrontPage();
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getCacheableMetadata() {
  39. $metadata = new CacheableMetadata();
  40. $metadata->addCacheTags(['config:system.site']);
  41. return $metadata;
  42. }
  43. }