Chris@0: $value) { Chris@0: $context_definition = $this->getContextDefinition($key); Chris@0: $contexts[$key] = new Context($context_definition, $value); Chris@0: } Chris@0: return $contexts; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * This code is identical to the Component in order to pick up a different Chris@0: * Context class. Chris@0: * Chris@0: * @return \Drupal\Core\Plugin\Context\ContextInterface Chris@0: * The context object. Chris@0: */ Chris@0: public function getContext($name) { Chris@0: // Check for a valid context value. Chris@0: if (!isset($this->context[$name])) { Chris@0: $this->context[$name] = new Context($this->getContextDefinition($name)); Chris@0: } Chris@0: return $this->context[$name]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setContext($name, ComponentContextInterface $context) { Chris@0: // Check that the context passed is an instance of our extended interface. Chris@0: if (!$context instanceof ContextInterface) { Chris@0: throw new ContextException("Passed $name context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface"); Chris@0: } Chris@0: parent::setContext($name, $context); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setContextValue($name, $value) { Chris@17: $this->setContext($name, Context::createFromContext($this->getContext($name), $value)); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getContextMapping() { Chris@18: $configuration = PluginHelper::isConfigurable($this) ? $this->getConfiguration() : $this->configuration; Chris@0: return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setContextMapping(array $context_mapping) { Chris@18: if (PluginHelper::isConfigurable($this)) { Chris@0: $configuration = $this->getConfiguration(); Chris@0: $configuration['context_mapping'] = array_filter($context_mapping); Chris@0: $this->setConfiguration($configuration); Chris@0: } Chris@0: else { Chris@0: $this->configuration['context_mapping'] = $context_mapping; Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface[] Chris@0: */ Chris@0: public function getContextDefinitions() { Chris@0: return parent::getContextDefinitions(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: * Chris@0: * @return \Drupal\Core\Plugin\Context\ContextDefinitionInterface Chris@0: */ Chris@0: public function getContextDefinition($name) { Chris@0: return parent::getContextDefinition($name); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps the context handler. Chris@0: * Chris@0: * @return \Drupal\Core\Plugin\Context\ContextHandlerInterface Chris@0: */ Chris@0: protected function contextHandler() { Chris@0: return \Drupal::service('context.handler'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheContexts() { Chris@0: $cache_contexts = []; Chris@0: // Applied contexts can affect the cache contexts when this plugin is Chris@0: // involved in caching, collect and return them. Chris@0: foreach ($this->getContexts() as $context) { Chris@0: /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */ Chris@0: if ($context instanceof CacheableDependencyInterface) { Chris@0: $cache_contexts = Cache::mergeContexts($cache_contexts, $context->getCacheContexts()); Chris@0: } Chris@0: } Chris@0: return $cache_contexts; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheTags() { Chris@0: $tags = []; Chris@0: // Applied contexts can affect the cache tags when this plugin is Chris@0: // involved in caching, collect and return them. Chris@0: foreach ($this->getContexts() as $context) { Chris@0: /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */ Chris@0: if ($context instanceof CacheableDependencyInterface) { Chris@0: $tags = Cache::mergeTags($tags, $context->getCacheTags()); Chris@0: } Chris@0: } Chris@0: return $tags; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getCacheMaxAge() { Chris@0: $max_age = Cache::PERMANENT; Chris@0: Chris@0: // Applied contexts can affect the cache max age when this plugin is Chris@0: // involved in caching, collect and return them. Chris@0: foreach ($this->getContexts() as $context) { Chris@0: /** @var $context \Drupal\Core\Cache\CacheableDependencyInterface */ Chris@0: if ($context instanceof CacheableDependencyInterface) { Chris@0: $max_age = Cache::mergeMaxAges($max_age, $context->getCacheMaxAge()); Chris@0: } Chris@0: } Chris@0: return $max_age; Chris@0: } Chris@0: Chris@0: }