comparison core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Plugin;
4
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;
16
17 /**
18 * Base class for plugins that are context aware.
19 */
20 abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
21 use TypedDataTrait;
22 use StringTranslationTrait;
23 use DependencySerializationTrait;
24
25 /**
26 * {@inheritdoc}
27 *
28 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
29 */
30 protected function createContextFromConfiguration(array $context_configuration) {
31 // This method is overridden so that it will use
32 // \Drupal\Core\Plugin\Context\Context instead.
33 $contexts = [];
34 foreach ($context_configuration as $key => $value) {
35 $context_definition = $this->getContextDefinition($key);
36 $contexts[$key] = new Context($context_definition, $value);
37 }
38 return $contexts;
39 }
40
41 /**
42 * {@inheritdoc}
43 *
44 * This code is identical to the Component in order to pick up a different
45 * Context class.
46 *
47 * @return \Drupal\Core\Plugin\Context\ContextInterface
48 * The context object.
49 */
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));
54 }
55 return $this->context[$name];
56 }
57
58 /**
59 * {@inheritdoc}
60 */
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");
65 }
66 parent::setContext($name, $context);
67 }
68
69 /**
70 * {@inheritdoc}
71 */
72 public function setContextValue($name, $value) {
73 $this->context[$name] = Context::createFromContext($this->getContext($name), $value);
74 return $this;
75 }
76
77 /**
78 * {@inheritdoc}
79 */
80 public function getContextMapping() {
81 $configuration = $this instanceof ConfigurablePluginInterface ? $this->getConfiguration() : $this->configuration;
82 return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
83 }
84
85 /**
86 * {@inheritdoc}
87 */
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);
93 }
94 else {
95 $this->configuration['context_mapping'] = $context_mapping;
96 }
97 return $this;
98 }
99
100 /**
101 * {@inheritdoc}
102 *
103 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
104 */
105 public function getContextDefinitions() {
106 return parent::getContextDefinitions();
107 }
108
109 /**
110 * {@inheritdoc}
111 *
112 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
113 */
114 public function getContextDefinition($name) {
115 return parent::getContextDefinition($name);
116 }
117
118 /**
119 * Wraps the context handler.
120 *
121 * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
122 */
123 protected function contextHandler() {
124 return \Drupal::service('context.handler');
125 }
126
127 /**
128 * {@inheritdoc}
129 */
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());
138 }
139 }
140 return $cache_contexts;
141 }
142
143 /**
144 * {@inheritdoc}
145 */
146 public function getCacheTags() {
147 $tags = [];
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());
154 }
155 }
156 return $tags;
157 }
158
159 /**
160 * {@inheritdoc}
161 */
162 public function getCacheMaxAge() {
163 $max_age = Cache::PERMANENT;
164
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());
171 }
172 }
173 return $max_age;
174 }
175
176 }