3 namespace Drupal\Core\Plugin\Context;
5 use Drupal\Component\Plugin\Context\Context as ComponentContext;
6 use Drupal\Component\Plugin\Exception\ContextException;
7 use Drupal\Core\Cache\CacheableDependencyInterface;
8 use Drupal\Core\Cache\CacheableMetadata;
9 use Drupal\Core\TypedData\TypedDataInterface;
10 use Drupal\Core\TypedData\TypedDataTrait;
13 * A Drupal specific context wrapper class.
15 class Context extends ComponentContext implements ContextInterface {
20 * The data associated with the context.
22 * @var \Drupal\Core\TypedData\TypedDataInterface
24 protected $contextData;
27 * The definition to which a context must conform.
29 * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
31 protected $contextDefinition;
34 * The cacheability metadata.
36 * @var \Drupal\Core\Cache\CacheableMetadata
38 protected $cacheabilityMetadata;
41 * Create a context object.
43 * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
44 * The context definition.
45 * @param mixed $context_value|null
46 * The context value object.
48 public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
49 parent::__construct($context_definition, NULL);
50 $this->cacheabilityMetadata = new CacheableMetadata();
51 if (!is_null($context_value)) {
52 $this->setContextValue($context_value);
59 public function getContextValue() {
60 if (!isset($this->contextData)) {
61 $definition = $this->getContextDefinition();
62 $default_value = $definition->getDefaultValue();
64 if (isset($default_value)) {
65 // Keep the default value here so that subsequent calls don't have to
67 $this->setContextValue($default_value);
69 elseif ($definition->isRequired()) {
70 $type = $definition->getDataType();
71 throw new ContextException("The '$type' context is required and not present.");
73 return $default_value;
75 return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
81 public function hasContextValue() {
82 return (bool) $this->contextData || parent::hasContextValue();
86 * Sets the context value.
89 * The value of this context, matching the context definition.
91 protected function setContextValue($value) {
92 // Add the value as a cacheable dependency only if implements the interface
93 // to prevent it from disabling caching with a max-age 0.
94 if ($value instanceof CacheableDependencyInterface) {
95 $this->addCacheableDependency($value);
97 if ($value instanceof TypedDataInterface) {
98 $this->contextData = $value;
101 $this->contextData = $this->getTypedDataManager()->create($this->contextDefinition->getDataDefinition(), $value);
108 public function getConstraints() {
109 return $this->contextDefinition->getConstraints();
115 public function getContextData() {
116 if (!isset($this->contextData)) {
117 $definition = $this->getContextDefinition();
118 $default_value = $definition->getDefaultValue();
119 // Store the default value so that subsequent calls don't have to look
121 $this->contextData = $this->getTypedDataManager()->create($definition->getDataDefinition(), $default_value);
123 return $this->contextData;
130 public function getContextDefinition() {
131 return $this->contextDefinition;
137 public function validate() {
138 return $this->getContextData()->validate();
144 public function addCacheableDependency($dependency) {
145 $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));
152 public function getCacheContexts() {
153 return $this->cacheabilityMetadata->getCacheContexts();
159 public function getCacheTags() {
160 return $this->cacheabilityMetadata->getCacheTags();
166 public function getCacheMaxAge() {
167 return $this->cacheabilityMetadata->getCacheMaxAge();
173 public static function createFromContext(ContextInterface $old_context, $value) {
174 $context = new static($old_context->getContextDefinition(), $value);
175 $context->addCacheableDependency($old_context);
176 if (method_exists($old_context, 'getTypedDataManager')) {
177 $context->setTypedDataManager($old_context->getTypedDataManager());