3 namespace Drupal\config_override_test\Cache;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\Context\CacheContextInterface;
9 * Defines the PirateDayCacheContext service that allows to cache the booty.
11 * Cache context ID: 'pirate_day'.
13 class PirateDayCacheContext implements CacheContextInterface {
16 * The length of Pirate Day. It lasts 24 hours.
18 * This is a simplified test implementation. In a real life Pirate Day module
19 * this data wouldn't be defined in a constant, but calculated in a static
20 * method. If it were Pirate Day it should return the number of seconds until
21 * midnight, and on all other days it should return the number of seconds
22 * until the start of the next Pirate Day.
24 const PIRATE_DAY_MAX_AGE = 86400;
29 public static function getLabel() {
30 return t('Pirate day');
36 public function getContext() {
37 $is_pirate_day = static::isPirateDay() ? 'yarr' : 'nay';
38 return "pirate_day." . $is_pirate_day;
42 * Returns whether or not it is Pirate Day.
44 * To ease testing this is determined with a global variable rather than using
45 * the traditional compass and sextant.
48 * Returns TRUE if it is Pirate Day today.
50 public static function isPirateDay() {
51 return !empty($GLOBALS['it_is_pirate_day']);
57 public function getCacheableMetadata() {
58 return new CacheableMetadata();