ContextAwarePluginTrait.php in Drupal 10
File
core/lib/Drupal/Core/Plugin/ContextAwarePluginTrait.php
View source
<?php
namespace Drupal\Core\Plugin;
use Drupal\Component\Plugin\ConfigurableInterface;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextInterface;
use Symfony\Component\Validator\ConstraintViolationList;
trait ContextAwarePluginTrait {
protected $context = [];
public function getContexts() {
foreach ($this
->getContextDefinitions() as $name => $definition) {
$this
->getContext($name);
}
return $this->context;
}
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");
}
$this->context[$name] = $context;
}
public function getContextValues() {
$values = [];
foreach ($this
->getContextDefinitions() as $name => $definition) {
$values[$name] = isset($this->context[$name]) ? $this->context[$name]
->getContextValue() : NULL;
}
return $values;
}
public function getContextValue($name) {
return $this
->getContext($name)
->getContextValue();
}
public function setContextValue($name, $value) {
$this
->setContext($name, Context::createFromContext($this
->getContext($name), $value));
return $this;
}
public function getContextMapping() {
$configuration = $this instanceof ConfigurableInterface ? $this
->getConfiguration() : $this->configuration;
return $configuration['context_mapping'] ?? [];
}
public function setContextMapping(array $context_mapping) {
if ($this instanceof ConfigurableInterface) {
$configuration = $this
->getConfiguration();
$configuration['context_mapping'] = array_filter($context_mapping);
$this
->setConfiguration($configuration);
}
else {
$this->configuration['context_mapping'] = $context_mapping;
}
return $this;
}
public abstract function getPluginDefinition();
public function getContextDefinitions() {
$definition = $this
->getPluginDefinition();
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
return $definition
->getContextDefinitions();
}
return !empty($definition['context_definitions']) ? $definition['context_definitions'] : [];
}
public function getContextDefinition($name) {
$definition = $this
->getPluginDefinition();
if ($definition instanceof ContextAwarePluginDefinitionInterface) {
if ($definition
->hasContextDefinition($name)) {
return $definition
->getContextDefinition($name);
}
}
elseif (!empty($definition['context_definitions'][$name])) {
return $definition['context_definitions'][$name];
}
throw new ContextException(sprintf("The %s context is not a valid context.", $name));
}
public function validateContexts() {
$violations = new ConstraintViolationList();
foreach ($this
->getContexts() as $context) {
$violations
->addAll($context
->validate());
}
return $violations;
}
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;
}
}