Context
class Context extends Context implements ContextInterface (View source)
A Drupal specific context wrapper class.
Traits
Wrapper methods for classes that needs typed data manager object.
Provides dependency injection friendly methods for serialization.
Properties
| protected mixed | $contextValue | The value of the context. |
from Context |
| protected ContextDefinitionInterface | $contextDefinition | The definition to which a context must conform. |
|
| protected TypedDataManagerInterface | $typedDataManager | The typed data manager used for creating the data types. |
from TypedDataTrait |
| protected array | $_serviceIds | An array of service IDs keyed by property name used for serialization. |
from DependencySerializationTrait |
| protected array | $_entityStorages | An array of entity type IDs keyed by the property name of their storages. |
from DependencySerializationTrait |
| protected TypedDataInterface | $contextData | The data associated with the context. |
|
| protected CacheableMetadata | $cacheabilityMetadata | The cacheability metadata. |
Methods
Create a context object.
Gets the context value.
Returns whether the context has a value.
Gets the provided definition that the context must conform to.
Gets a list of validation constraints.
Validates the set context value.
Sets the context value.
Gets the context value as typed data object.
Adds a dependency on an object: merges its cacheability metadata.
The cache contexts associated with this object.
The cache tags associated with this object.
The maximum age for which this object may be cached.
Creates a new context with a different value.
Details
__construct(ContextDefinitionInterface $context_definition, mixed|null $context_value = NULL)
Create a context object.
mixed
getContextValue()
Gets the context value.
bool
hasContextValue()
Returns whether the context has a value.
ContextDefinitionInterface
getContextDefinition()
Gets the provided definition that the context must conform to.
array
getConstraints()
Gets a list of validation constraints.
ConstraintViolationListInterface
validate()
Validates the set context value.
$this
setTypedDataManager(TypedDataManagerInterface $typed_data_manager)
Sets the typed data manager.
TypedDataManagerInterface
getTypedDataManager()
Gets the typed data manager.
__sleep()
{@inheritdoc}
__wakeup()
{@inheritdoc}
protected
setContextValue(mixed $value)
Sets the context value.
TypedDataInterface
getContextData()
Gets the context value as typed data object.
$this
addCacheableDependency(CacheableDependencyInterface|mixed $dependency)
Adds a dependency on an object: merges its cacheability metadata.
For example, when a context depends on some configuration, an entity, or an access result, we must make sure their cacheability metadata is present on the response. This method makes doing that simple.
string[]
getCacheContexts()
The cache contexts associated with this object.
These identify a specific variation/representation of the object.
Cache contexts are tokens: placeholders that are converted to cache keys by the @cache_contexts_manager service. The replacement value depends on the request context (the current URL, language, and so on). They're converted before storing an object in cache.
string[]
getCacheTags()
The cache tags associated with this object.
When this object is modified, these cache tags will be invalidated.
int
getCacheMaxAge()
The maximum age for which this object may be cached.
static ContextInterface
createFromContext(ContextInterface $old_context, mixed $value)
Creates a new context with a different value.