class Context
Same name in this branch
- 8.9.x core/lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
Same name in other branches
- 9 core/lib/Drupal/Core/Plugin/Context/Context.php \Drupal\Core\Plugin\Context\Context
- 9 core/lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
- 10 core/lib/Drupal/Core/Plugin/Context/Context.php \Drupal\Core\Plugin\Context\Context
- 10 core/lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
- 11.x core/lib/Drupal/Core/Plugin/Context/Context.php \Drupal\Core\Plugin\Context\Context
- 11.x core/lib/Drupal/Component/Plugin/Context/Context.php \Drupal\Component\Plugin\Context\Context
A Drupal specific context wrapper class.
Hierarchy
- class \Drupal\Component\Plugin\Context\Context implements \Drupal\Component\Plugin\Context\ContextInterface
- class \Drupal\Core\Plugin\Context\Context extends \Drupal\Component\Plugin\Context\Context implements \Drupal\Core\Plugin\Context\ContextInterface uses \Drupal\Core\TypedData\TypedDataTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait
Expanded class hierarchy of Context
25 files declare their use of Context
- ContextAwarePluginBase.php in core/
lib/ Drupal/ Core/ Plugin/ ContextAwarePluginBase.php - ContextDefinitionIsSatisfiedTest.php in core/
tests/ Drupal/ Tests/ Core/ Plugin/ Context/ ContextDefinitionIsSatisfiedTest.php - ContextTest.php in core/
tests/ Drupal/ Tests/ Core/ Plugin/ Context/ ContextTest.php - Contains \Drupal\Tests\Core\Plugin\Context\ContextTest.
- ContextTypedDataTest.php in core/
tests/ Drupal/ KernelTests/ Core/ Plugin/ ContextTypedDataTest.php - CurrentLanguageContext.php in core/
lib/ Drupal/ Core/ Language/ ContextProvider/ CurrentLanguageContext.php
19 string references to 'Context'
- AccessResultTest::testCacheUntilConfigurationChanges in core/
tests/ Drupal/ Tests/ Core/ Access/ AccessResultTest.php - @expectedDeprecation Drupal\Core\Access\AccessResult::cacheUntilConfigurationChanges is deprecated in drupal:8.0.0 and is removed in drupal:9.0.0. Use \Drupal\Core\Access\AccessResult::addCacheableDependency() instead. @group legacy
- AccessResultTest::testCacheUntilEntityChanges in core/
tests/ Drupal/ Tests/ Core/ Access/ AccessResultTest.php - @expectedDeprecation Drupal\Core\Access\AccessResult::cacheUntilEntityChanges is deprecated in drupal:8.0.0 and is removed in drupal:9.0.0. Use \Drupal\Core\Access\AccessResult::addCacheableDependency() instead. @group legacy
- ContextualLinkDefaultTest::testGetTitleWithContext in core/
tests/ Drupal/ Tests/ Core/ Menu/ ContextualLinkDefaultTest.php - @covers ::getTitle
- drupal7.php in core/
modules/ migrate_drupal/ tests/ fixtures/ drupal7.php - A database agnostic dump for testing purposes.
- EntityManagerTest::testGetTranslationFromContext in core/
tests/ Drupal/ Tests/ Core/ Entity/ EntityManagerTest.php - Tests the getTranslationFromContext() method.
File
-
core/
lib/ Drupal/ Core/ Plugin/ Context/ Context.php, line 16
Namespace
Drupal\Core\Plugin\ContextView source
class Context extends ComponentContext implements ContextInterface {
use TypedDataTrait;
use DependencySerializationTrait;
/**
* The data associated with the context.
*
* @var \Drupal\Core\TypedData\TypedDataInterface
*/
protected $contextData;
/**
* The definition to which a context must conform.
*
* @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
*/
protected $contextDefinition;
/**
* The cacheability metadata.
*
* @var \Drupal\Core\Cache\CacheableMetadata
*/
protected $cacheabilityMetadata;
/**
* Create a context object.
*
* @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
* The context definition.
* @param mixed|null $context_value
* The context value object.
*/
public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
parent::__construct($context_definition, NULL);
$this->cacheabilityMetadata = new CacheableMetadata();
if (!is_null($context_value)) {
$this->setContextValue($context_value);
}
}
/**
* {@inheritdoc}
*/
public function getContextValue() {
if (!isset($this->contextData)) {
$definition = $this->getContextDefinition();
$default_value = $definition->getDefaultValue();
if (isset($default_value)) {
// Keep the default value here so that subsequent calls don't have to
// look it up again.
$this->setContextValue($default_value);
}
elseif ($definition->isRequired()) {
$type = $definition->getDataType();
throw new ContextException("The '{$type}' context is required and not present.");
}
return $default_value;
}
return $this->getTypedDataManager()
->getCanonicalRepresentation($this->contextData);
}
/**
* {@inheritdoc}
*/
public function hasContextValue() {
return (bool) $this->contextData || parent::hasContextValue();
}
/**
* Sets the context value.
*
* @param mixed $value
* The value of this context, matching the context definition.
*/
protected function setContextValue($value) {
// Add the value as a cacheable dependency only if implements the interface
// to prevent it from disabling caching with a max-age 0.
if ($value instanceof CacheableDependencyInterface) {
$this->addCacheableDependency($value);
}
if ($value instanceof TypedDataInterface) {
$this->contextData = $value;
}
else {
$this->contextData = $this->getTypedDataManager()
->create($this->contextDefinition
->getDataDefinition(), $value);
}
}
/**
* {@inheritdoc}
*/
public function getConstraints() {
return $this->contextDefinition
->getConstraints();
}
/**
* {@inheritdoc}
*/
public function getContextData() {
if (!isset($this->contextData)) {
$definition = $this->getContextDefinition();
$default_value = $definition->getDefaultValue();
// Store the default value so that subsequent calls don't have to look
// it up again.
$this->contextData = $this->getTypedDataManager()
->create($definition->getDataDefinition(), $default_value);
}
return $this->contextData;
}
/**
* {@inheritdoc}
*/
public function getContextDefinition() {
return $this->contextDefinition;
}
/**
* {@inheritdoc}
*/
public function validate() {
return $this->getContextData()
->validate();
}
/**
* {@inheritdoc}
*/
public function addCacheableDependency($dependency) {
$this->cacheabilityMetadata = $this->cacheabilityMetadata
->merge(CacheableMetadata::createFromObject($dependency));
return $this;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return $this->cacheabilityMetadata
->getCacheContexts();
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return $this->cacheabilityMetadata
->getCacheTags();
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return $this->cacheabilityMetadata
->getCacheMaxAge();
}
/**
* {@inheritdoc}
*/
public static function createFromContext(ContextInterface $old_context, $value) {
$context = new static($old_context->getContextDefinition(), $value);
$context->addCacheableDependency($old_context);
if (method_exists($old_context, 'getTypedDataManager')) {
$context->setTypedDataManager($old_context->getTypedDataManager());
}
return $context;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
Context::$cacheabilityMetadata | protected | property | The cacheability metadata. | ||
Context::$contextData | protected | property | The data associated with the context. | ||
Context::$contextDefinition | protected | property | The definition to which a context must conform. | Overrides Context::$contextDefinition | |
Context::$contextValue | protected | property | The value of the context. | ||
Context::addCacheableDependency | public | function | Adds a dependency on an object: merges its cacheability metadata. | Overrides ContextInterface::addCacheableDependency | |
Context::createFromContext | public static | function | Creates a new context with a different value. | Overrides ContextInterface::createFromContext | |
Context::getCacheContexts | public | function | The cache contexts associated with this object. | Overrides CacheableDependencyInterface::getCacheContexts | |
Context::getCacheMaxAge | public | function | The maximum age for which this object may be cached. | Overrides CacheableDependencyInterface::getCacheMaxAge | |
Context::getCacheTags | public | function | The cache tags associated with this object. | Overrides CacheableDependencyInterface::getCacheTags | |
Context::getConstraints | public | function | Gets a list of validation constraints. | Overrides Context::getConstraints | |
Context::getContextData | public | function | Gets the context value as typed data object. | Overrides ContextInterface::getContextData | |
Context::getContextDefinition | public | function | Gets the provided definition that the context must conform to. | Overrides Context::getContextDefinition | |
Context::getContextValue | public | function | Gets the context value. | Overrides Context::getContextValue | |
Context::hasContextValue | public | function | Returns whether the context has a value. | Overrides Context::hasContextValue | |
Context::setContextValue | protected | function | Sets the context value. | ||
Context::validate | public | function | Validates the set context value. | Overrides Context::validate | |
Context::__construct | public | function | Create a context object. | Overrides Context::__construct | |
DependencySerializationTrait::$_entityStorages | protected | property | An array of entity type IDs keyed by the property name of their storages. | ||
DependencySerializationTrait::$_serviceIds | protected | property | An array of service IDs keyed by property name used for serialization. | ||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
TypedDataTrait::$typedDataManager | protected | property | The typed data manager used for creating the data types. | ||
TypedDataTrait::getTypedDataManager | public | function | Gets the typed data manager. | 2 | |
TypedDataTrait::setTypedDataManager | public | function | Sets the typed data manager. | 2 |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.