annotate core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Plugin;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
Chris@0 6 use Drupal\Component\Plugin\Exception\ContextException;
Chris@18 7 use Drupal\Component\Plugin\PluginHelper;
Chris@0 8 use Drupal\Core\Cache\Cache;
Chris@0 9 use Drupal\Core\Cache\CacheableDependencyInterface;
Chris@0 10 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
Chris@0 11 use Drupal\Core\Plugin\Context\Context;
Chris@0 12 use Drupal\Core\StringTranslation\StringTranslationTrait;
Chris@0 13 use Drupal\Core\TypedData\TypedDataTrait;
Chris@0 14 use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
Chris@0 15 use Drupal\Core\Plugin\Context\ContextInterface;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Base class for plugins that are context aware.
Chris@0 19 */
Chris@0 20 abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
Chris@0 21 use TypedDataTrait;
Chris@0 22 use StringTranslationTrait;
Chris@0 23 use DependencySerializationTrait;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * {@inheritdoc}
Chris@0 27 *
Chris@0 28 * @return \Drupal\Core\Plugin\Context\ContextInterface[]
Chris@0 29 */
Chris@0 30 protected function createContextFromConfiguration(array $context_configuration) {
Chris@0 31 // This method is overridden so that it will use
Chris@0 32 // \Drupal\Core\Plugin\Context\Context instead.
Chris@0 33 $contexts = [];
Chris@0 34 foreach ($context_configuration as $key => $value) {
Chris@0 35 $context_definition = $this->getContextDefinition($key);
Chris@0 36 $contexts[$key] = new Context($context_definition, $value);
Chris@0 37 }
Chris@0 38 return $contexts;
Chris@0 39 }
Chris@0 40
Chris@0 41 /**
Chris@0 42 * {@inheritdoc}
Chris@0 43 *
Chris@0 44 * This code is identical to the Component in order to pick up a different
Chris@0 45 * Context class.
Chris@0 46 *
Chris@0 47 * @return \Drupal\Core\Plugin\Context\ContextInterface
Chris@0 48 * The context object.
Chris@0 49 */
Chris@0 50 public function getContext($name) {
Chris@0 51 // Check for a valid context value.
Chris@0 52 if (!isset($this->context[$name])) {
Chris@0 53 $this->context[$name] = new Context($this->getContextDefinition($name));
Chris@0 54 }
Chris@0 55 return $this->context[$name];
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public function setContext($name, ComponentContextInterface $context) {
Chris@0 62 // Check that the context passed is an instance of our extended interface.
Chris@0 63 if (!$context instanceof ContextInterface) {
Chris@0 64 throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
Chris@0 65 }
Chris@0 66 parent::setContext($name, $context);
Chris@0 67 }
Chris@0 68
Chris@0 69 /**
Chris@0 70 * {@inheritdoc}
Chris@0 71 */
Chris@0 72 public function setContextValue($name, $value) {
Chris@17 73 $this->setContext($name, Context::createFromContext($this->getContext($name), $value));
Chris@0 74 return $this;
Chris@0 75 }
Chris@0 76
Chris@0 77 /**
Chris@0 78 * {@inheritdoc}
Chris@0 79 */
Chris@0 80 public function getContextMapping() {
Chris@18 81 $configuration = PluginHelper::isConfigurable($this) ? $this->getConfiguration() : $this->configuration;
Chris@0 82 return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
Chris@0 83 }
Chris@0 84
Chris@0 85 /**
Chris@0 86 * {@inheritdoc}
Chris@0 87 */
Chris@0 88 public function setContextMapping(array $context_mapping) {
Chris@18 89 if (PluginHelper::isConfigurable($this)) {
Chris@0 90 $configuration = $this->getConfiguration();
Chris@0 91 $configuration['context_mapping'] = array_filter($context_mapping);
Chris@0 92 $this->setConfiguration($configuration);
Chris@0 93 }
Chris@0 94 else {
Chris@0 95 $this->configuration['context_mapping'] = $context_mapping;
Chris@0 96 }
Chris@0 97 return $this;
Chris@0 98 }
Chris@0 99
Chris@0 100 /**
Chris@0 101 * {@inheritdoc}
Chris@0 102 *
Chris@0 103 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[]
Chris@0 104 */
Chris@0 105 public function getContextDefinitions() {
Chris@0 106 return parent::getContextDefinitions();
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * {@inheritdoc}
Chris@0 111 *
Chris@0 112 * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface
Chris@0 113 */
Chris@0 114 public function getContextDefinition($name) {
Chris@0 115 return parent::getContextDefinition($name);
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Wraps the context handler.
Chris@0 120 *
Chris@0 121 * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface
Chris@0 122 */
Chris@0 123 protected function contextHandler() {
Chris@0 124 return \Drupal::service('context.handler');
Chris@0 125 }
Chris@0 126
Chris@0 127 /**
Chris@0 128 * {@inheritdoc}
Chris@0 129 */
Chris@0 130 public function getCacheContexts() {
Chris@0 131 $cache_contexts = [];
Chris@0 132 // Applied contexts can affect the cache contexts when this plugin is
Chris@0 133 // involved in caching, collect and return them.
Chris@0 134 foreach ($this->getContexts() as $context) {
Chris@0 135 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
Chris@0 136 if ($context instanceof CacheableDependencyInterface) {
Chris@0 137 $cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts());
Chris@0 138 }
Chris@0 139 }
Chris@0 140 return $cache_contexts;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * {@inheritdoc}
Chris@0 145 */
Chris@0 146 public function getCacheTags() {
Chris@0 147 $tags = [];
Chris@0 148 // Applied contexts can affect the cache tags when this plugin is
Chris@0 149 // involved in caching, collect and return them.
Chris@0 150 foreach ($this->getContexts() as $context) {
Chris@0 151 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
Chris@0 152 if ($context instanceof CacheableDependencyInterface) {
Chris@0 153 $tags = Cache::mergeTags($tags, $context->getCacheTags());
Chris@0 154 }
Chris@0 155 }
Chris@0 156 return $tags;
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * {@inheritdoc}
Chris@0 161 */
Chris@0 162 public function getCacheMaxAge() {
Chris@0 163 $max_age = Cache::PERMANENT;
Chris@0 164
Chris@0 165 // Applied contexts can affect the cache max age when this plugin is
Chris@0 166 // involved in caching, collect and return them.
Chris@0 167 foreach ($this->getContexts() as $context) {
Chris@0 168 /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */
Chris@0 169 if ($context instanceof CacheableDependencyInterface) {
Chris@0 170 $max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge());
Chris@0 171 }
Chris@0 172 }
Chris@0 173 return $max_age;
Chris@0 174 }
Chris@0 175
Chris@0 176 }