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

__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.

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.

getTypedDataManager()

Gets the typed data manager.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

setContextValue(mixed $value)

Sets the context value.

getContextData()

Gets the context value as typed data object.

$this
addCacheableDependency(CacheableDependencyInterface|mixed $dependency)

Adds a dependency on an object: merges its cacheability metadata.

string[]
getCacheContexts()

The cache contexts associated with this object.

string[]
getCacheTags()

The cache tags associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

createFromContext(ContextInterface $old_context, mixed $value)

Creates a new context with a different value.

Details

__construct(ContextDefinitionInterface $context_definition, mixed|null $context_value = NULL)

Create a context object.

Parameters

ContextDefinitionInterface $context_definition

The context definition.

mixed|null $context_value

The value of the context.

mixed getContextValue()

Gets the context value.

Return Value

mixed

The currently set context value, or NULL if it is not set.

bool hasContextValue()

Returns whether the context has a value.

Return Value

bool

TRUE if the context has a value, FALSE otherwise.

ContextDefinitionInterface getContextDefinition()

Gets the provided definition that the context must conform to.

Return Value

ContextDefinitionInterface

The defining characteristic representation of the context.

array getConstraints()

Gets a list of validation constraints.

Return Value

array

Array of constraints, each being an instance of \Symfony\Component\Validator\Constraint.

ConstraintViolationListInterface validate()

Validates the set context value.

Return Value

ConstraintViolationListInterface

A list of constraint violations. If the list is empty, validation succeeded.

$this setTypedDataManager(TypedDataManagerInterface $typed_data_manager)

Sets the typed data manager.

Parameters

TypedDataManagerInterface $typed_data_manager

The typed data manager.

Return Value

$this

TypedDataManagerInterface getTypedDataManager()

Gets the typed data manager.

Return Value

TypedDataManagerInterface

The typed data manager.

__sleep()

{@inheritdoc}

__wakeup()

{@inheritdoc}

protected setContextValue(mixed $value)

Sets the context value.

Parameters

mixed $value

The value of this context, matching the context definition.

TypedDataInterface getContextData()

Gets the context value as typed data object.

Return Value

TypedDataInterface

$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.

Parameters

CacheableDependencyInterface|mixed $dependency

The dependency. If the object implements CacheableDependencyInterface, then its cacheability metadata will be used. Otherwise, the passed in object must be assumed to be uncacheable, so max-age 0 is set.

Return Value

$this

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.

Return Value

string[]

An array of cache context tokens, used to generate a cache ID.

string[] getCacheTags()

The cache tags associated with this object.

When this object is modified, these cache tags will be invalidated.

Return Value

string[]

A set of cache tags.

int getCacheMaxAge()

The maximum age for which this object may be cached.

Return Value

int

The maximum time in seconds that this object may be cached.

static ContextInterface createFromContext(ContextInterface $old_context, mixed $value)

Creates a new context with a different value.

Parameters

ContextInterface $old_context

The context object used to create a new object. Cacheability metadata will be copied over.

mixed $value

The value of the new context object.

Return Value

ContextInterface