ThemeCacheContext.php 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace Drupal\Core\Cache\Context;
  3. use Drupal\Core\Cache\CacheableMetadata;
  4. use Drupal\Core\Theme\ThemeManagerInterface;
  5. /**
  6. * Defines the ThemeCacheContext service, for "per theme" caching.
  7. *
  8. * Cache context ID: 'theme'.
  9. */
  10. class ThemeCacheContext implements CacheContextInterface {
  11. /**
  12. * The theme manager.
  13. *
  14. * @var \Drupal\Core\Theme\ThemeManagerInterface
  15. */
  16. protected $themeManager;
  17. /**
  18. * Constructs a new ThemeCacheContext service.
  19. *
  20. * @param \Drupal\Core\Theme\ThemeManagerInterface $theme_manager
  21. * The theme manager.
  22. */
  23. public function __construct(ThemeManagerInterface $theme_manager) {
  24. $this->themeManager = $theme_manager;
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public static function getLabel() {
  30. return t('Theme');
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getContext() {
  36. return $this->themeManager->getActiveTheme()->getName() ?: 'stark';
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function getCacheableMetadata() {
  42. return new CacheableMetadata();
  43. }
  44. }