ContextAwarePluginBase.php in Drupal 8
File
core/lib/Drupal/Core/Plugin/ContextAwarePluginBase.php
View source
<?php
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\ContextAwarePluginBase as ComponentContextAwarePluginBase;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\PluginHelper;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\TypedData\TypedDataTrait;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Core\Plugin\Context\ContextInterface;
abstract class ContextAwarePluginBase extends ComponentContextAwarePluginBase implements ContextAwarePluginInterface, CacheableDependencyInterface {
use TypedDataTrait;
use StringTranslationTrait;
use DependencySerializationTrait;
protected function createContextFromConfiguration(array $context_configuration) {
$contexts = [];
foreach ($context_configuration as $key => $value) {
$context_definition = $this
->getContextDefinition($key);
$contexts[$key] = new Context($context_definition, $value);
}
return $contexts;
}
public function getContext($name) {
if (!isset($this->context[$name])) {
$this->context[$name] = new Context($this
->getContextDefinition($name));
}
return $this->context[$name];
}
public function setContext($name, ComponentContextInterface $context) {
if (!$context instanceof ContextInterface) {
throw new ContextException("Passed {$name} context must be an instance of \\Drupal\\Core\\Plugin\\Context\\ContextInterface");
}
parent::setContext($name, $context);
}
public function setContextValue($name, $value) {
$this
->setContext($name, Context::createFromContext($this
->getContext($name), $value));
return $this;
}
public function getContextMapping() {
$configuration = PluginHelper::isConfigurable($this) ? $this
->getConfiguration() : $this->configuration;
return isset($configuration['context_mapping']) ? $configuration['context_mapping'] : [];
}
public function setContextMapping(array $context_mapping) {
if (PluginHelper::isConfigurable($this)) {
$configuration = $this
->getConfiguration();
$configuration['context_mapping'] = array_filter($context_mapping);
$this
->setConfiguration($configuration);
}
else {
$this->configuration['context_mapping'] = $context_mapping;
}
return $this;
}
public function getContextDefinitions() {
return parent::getContextDefinitions();
}
public function getContextDefinition($name) {
return parent::getContextDefinition($name);
}
protected function contextHandler() {
return \Drupal::service('context.handler');
}
public function getCacheContexts() {
$cache_contexts = [];
foreach ($this
->getContexts() as $context) {
if ($context instanceof CacheableDependencyInterface) {
$cache_contexts = Cache::mergeContexts($cache_contexts, $context
->getCacheContexts());
}
}
return $cache_contexts;
}
public function getCacheTags() {
$tags = [];
foreach ($this
->getContexts() as $context) {
if ($context instanceof CacheableDependencyInterface) {
$tags = Cache::mergeTags($tags, $context
->getCacheTags());
}
}
return $tags;
}
public function getCacheMaxAge() {
$max_age = Cache::PERMANENT;
foreach ($this
->getContexts() as $context) {
if ($context instanceof CacheableDependencyInterface) {
$max_age = Cache::mergeMaxAges($max_age, $context
->getCacheMaxAge());
}
}
return $max_age;
}
}