class CacheableMetadata implements RefinableCacheableDependencyInterface (View source)

Defines a generic class for passing cacheability metadata.

Traits

Trait for \Drupal\Core\Cache\RefinableCacheableDependencyInterface.

Trait for \Drupal\Core\Cache\CacheableDependencyInterface.

Properties

protected string[] $cacheContexts

Cache contexts.

from  CacheableDependencyTrait
protected string[] $cacheTags

Cache tags.

from  CacheableDependencyTrait
protected int $cacheMaxAge

Cache max-age.

from  CacheableDependencyTrait

Methods

$this
setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

string[]
getCacheTags()

The cache tags associated with this object.

string[]
getCacheContexts()

The cache contexts associated with this object.

int
getCacheMaxAge()

The maximum age for which this object may be cached.

addCacheableDependency($other_object)

{@inheritdoc}

addCacheContexts(array $cache_contexts)

{@inheritdoc}

addCacheTags(array $cache_tags)

{@inheritdoc}

$this
setCacheTags(array $cache_tags)

Sets cache tags.

$this
setCacheContexts(array $cache_contexts)

Sets cache contexts.

$this
setCacheMaxAge(int $max_age)

Sets the maximum age (in seconds).

merge(CacheableMetadata $other)

Merges the values of another CacheableMetadata object with this one.

applyTo(array $build)

Applies the values of this CacheableMetadata object to a render array.

createFromRenderArray(array $build)

Creates a CacheableMetadata object with values taken from a render array.

createFromObject(CacheableDependencyInterface|mixed $object)

Creates a CacheableMetadata object from a depended object.

Details

protected $this setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

Parameters

CacheableDependencyInterface $cacheability

The cacheability to set.

Return Value

$this

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.

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.

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.

addCacheableDependency($other_object)

{@inheritdoc}

Parameters

$other_object

addCacheContexts(array $cache_contexts)

{@inheritdoc}

Parameters

array $cache_contexts

addCacheTags(array $cache_tags)

{@inheritdoc}

Parameters

array $cache_tags

mergeCacheMaxAge($max_age)

{@inheritdoc}

Parameters

$max_age

$this setCacheTags(array $cache_tags)

Sets cache tags.

Parameters

array $cache_tags

The cache tags to be associated.

Return Value

$this

$this setCacheContexts(array $cache_contexts)

Sets cache contexts.

Parameters

array $cache_contexts

The cache contexts to be associated.

Return Value

$this

$this setCacheMaxAge(int $max_age)

Sets the maximum age (in seconds).

Defaults to Cache::PERMANENT

Parameters

int $max_age

The max age to associate.

Return Value

$this

Exceptions

InvalidArgumentException

CacheableMetadata merge(CacheableMetadata $other)

Merges the values of another CacheableMetadata object with this one.

Parameters

CacheableMetadata $other

The other CacheableMetadata object.

Return Value

CacheableMetadata

A new CacheableMetadata object, with the merged data.

applyTo(array $build)

Applies the values of this CacheableMetadata object to a render array.

Parameters

array $build

A render array.

static CacheableMetadata createFromRenderArray(array $build)

Creates a CacheableMetadata object with values taken from a render array.

Parameters

array $build

A render array.

Return Value

CacheableMetadata

static CacheableMetadata createFromObject(CacheableDependencyInterface|mixed $object)

Creates a CacheableMetadata object from a depended object.

Parameters

CacheableDependencyInterface|mixed $object

The object whose cacheability metadata to retrieve. If it implements CacheableDependencyInterface, 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

CacheableMetadata