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\DependencyInjection\DependencySerializationTrait;
10 use Drupal\Core\TypedData\TypedDataInterface;
11 use Drupal\Core\TypedData\TypedDataTrait;
14 * A Drupal specific context wrapper class.
16 class Context extends ComponentContext implements ContextInterface {
19 use DependencySerializationTrait;
22 * The data associated with the context.
24 * @var \Drupal\Core\TypedData\TypedDataInterface
26 protected $contextData;
29 * The definition to which a context must conform.
31 * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
33 protected $contextDefinition;
36 * The cacheability metadata.
38 * @var \Drupal\Core\Cache\CacheableMetadata
40 protected $cacheabilityMetadata;
43 * Create a context object.
45 * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
46 * The context definition.
47 * @param mixed|null $context_value
48 * The context value object.
50 public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
51 parent::__construct($context_definition, NULL);
52 $this->cacheabilityMetadata = new CacheableMetadata();
53 if (!is_null($context_value)) {
54 $this->setContextValue($context_value);
61 public function getContextValue() {
62 if (!isset($this->contextData)) {
63 $definition = $this->getContextDefinition();
64 $default_value = $definition->getDefaultValue();
66 if (isset($default_value)) {
67 // Keep the default value here so that subsequent calls don't have to
69 $this->setContextValue($default_value);
71 elseif ($definition->isRequired()) {
72 $type = $definition->getDataType();
73 throw new ContextException("The '$type' context is required and not present.");
75 return $default_value;
77 return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
83 public function hasContextValue() {
84 return (bool) $this->contextData || parent::hasContextValue();
88 * Sets the context value.
91 * The value of this context, matching the context definition.
93 protected function setContextValue($value) {
94 // Add the value as a cacheable dependency only if implements the interface
95 // to prevent it from disabling caching with a max-age 0.
96 if ($value instanceof CacheableDependencyInterface) {
97 $this->addCacheableDependency($value);
99 if ($value instanceof TypedDataInterface) {
100 $this->contextData = $value;
103 $this->contextData = $this->getTypedDataManager()->create($this->contextDefinition->getDataDefinition(), $value);
110 public function getConstraints() {
111 return $this->contextDefinition->getConstraints();
117 public function getContextData() {
118 if (!isset($this->contextData)) {
119 $definition = $this->getContextDefinition();
120 $default_value = $definition->getDefaultValue();
121 // Store the default value so that subsequent calls don't have to look
123 $this->contextData = $this->getTypedDataManager()->create($definition->getDataDefinition(), $default_value);
125 return $this->contextData;
131 public function getContextDefinition() {
132 return $this->contextDefinition;
138 public function validate() {
139 return $this->getContextData()->validate();
145 public function addCacheableDependency($dependency) {
146 $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));
153 public function getCacheContexts() {
154 return $this->cacheabilityMetadata->getCacheContexts();
160 public function getCacheTags() {
161 return $this->cacheabilityMetadata->getCacheTags();
167 public function getCacheMaxAge() {
168 return $this->cacheabilityMetadata->getCacheMaxAge();
174 public static function createFromContext(ContextInterface $old_context, $value) {
175 $context = new static($old_context->getContextDefinition(), $value);
176 $context->addCacheableDependency($old_context);
177 if (method_exists($old_context, 'getTypedDataManager')) {
178 $context->setTypedDataManager($old_context->getTypedDataManager());