3 namespace Drupal\Core\Plugin;
5 use Drupal\Component\Plugin\ConfigurablePluginInterface;
6 use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
7 use Drupal\Component\Plugin\Exception\ContextException;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\Cache\CacheableDependencyInterface;
10 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
11 use Drupal\Core\Plugin\Context\Context;
12 use Drupal\Core\StringTranslation\StringTranslationTrait;
13 use Drupal\Core\TypedData\TypedDataTrait;
14 use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
15 use Drupal\Core\Plugin\Context\ContextInterface;
18 * Base class for plugins that are context aware.
20 abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
22 use StringTranslationTrait;
23 use DependencySerializationTrait;
28 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
30 protected function createContextFromConfiguration(array $context_configuration) {
31 // This method is overridden so that it will use
32 // \Drupal\Core\Plugin\Context\Context instead.
34 foreach ($context_configuration as $key => $value) {
35 $context_definition = $this->getContextDefinition($key);
36 $contexts[$key] = new Context($context_definition, $value);
44 * This code is identical to the Component in order to pick up a different
47 * @return \Drupal\Core\Plugin\Context\ContextInterface
50 public function getContext($name) {
51 // Check for a valid context value.
52 if (!isset($this->context[$name])) {
53 $this->context[$name] = new Context($this->getContextDefinition($name));
55 return $this->context[$name];
61 public function setContext($name, ComponentContextInterface $context) {
62 // Check that the context passed is an instance of our extended interface.
63 if (!$context instanceof ContextInterface) {
64 throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
66 parent::setContext($name, $context);
72 public function setContextValue($name, $value) {
73 $this->context[$name] = Context::createFromContext($this->getContext($name), $value);
80 public function getContextMapping() {
81 $configuration = $this instanceof ConfigurablePluginInterface ? $this->getConfiguration() : $this->configuration;
82 return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
88 public function setContextMapping(array $context_mapping) {
89 if ($this instanceof ConfigurablePluginInterface) {
90 $configuration = $this->getConfiguration();
91 $configuration['context_mapping'] = array_filter($context_mapping);
92 $this->setConfiguration($configuration);
95 $this->configuration['context_mapping'] = $context_mapping;
103 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
105 public function getContextDefinitions() {
106 return parent::getContextDefinitions();
112 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
114 public function getContextDefinition($name) {
115 return parent::getContextDefinition($name);
119 * Wraps the context handler.
121 * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
123 protected function contextHandler() {
124 return \Drupal::service('context.handler');
130 public function getCacheContexts() {
131 $cache_contexts = [];
132 // Applied contexts can affect the cache contexts when this plugin is
133 // involved in caching, collect and return them.
134 foreach ($this->getContexts() as $context) {
135 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
136 if ($context instanceof CacheableDependencyInterface) {
137 $cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts());
140 return $cache_contexts;
146 public function getCacheTags() {
148 // Applied contexts can affect the cache tags when this plugin is
149 // involved in caching, collect and return them.
150 foreach ($this->getContexts() as $context) {
151 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
152 if ($context instanceof CacheableDependencyInterface) {
153 $tags = Cache::mergeTags($tags, $context->getCacheTags());
162 public function getCacheMaxAge() {
163 $max_age = Cache::PERMANENT;
165 // Applied contexts can affect the cache max age when this plugin is
166 // involved in caching, collect and return them.
167 foreach ($this->getContexts() as $context) {
168 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
169 if ($context instanceof CacheableDependencyInterface) {
170 $max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge());