class FilterProcessResult extends BubbleableMetadata (View source)

Used to return values from a text filter plugin's processing method.

The typical use case for a text filter plugin's processing method is to just apply some filtering to the given text, but for more advanced use cases, it may be necessary to also:

  • Declare asset libraries to be loaded.
  • Declare cache tags that the filtered text depends upon, so when either of those cache tags is invalidated, the filtered text should also be invalidated.
  • Declare cache context to vary by, e.g. 'language' to do language-specific filtering.
  • Declare a maximum age for the filtered text.
  • Apply uncacheable filtering, for example because it differs per user.

In case a filter needs one or more of these advanced use cases, it can use the additional methods available.

The typical use case:

Traits

Provides an implementation of AttachmentsInterface.

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
protected array $attachments

The attachments for this response.

from  AttachmentsTrait
protected string $processedText

The processed text.

Methods

$this
setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

getCacheTags()

{@inheritdoc}

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)

Creates a new bubbleable metadata object by merging this one with another.

applyTo(array $build)

Applies the values of this bubbleable metadata object to a render array.

createFromRenderArray(array $build)

Creates a bubbleable metadata object with values taken from a render array.

createFromObject(CacheableDependencyInterface|mixed $object)

Creates a bubbleable metadata object from a depended object.

getAttachments()

{@inheritdoc}

addAttachments(array $attachments)

{@inheritdoc}

setAttachments(array $attachments)

{@inheritdoc}

static array
mergeAttachments(array $a, array $b)

Merges two attachments arrays (which live under the '#attached' key).

__construct(string $processed_text = '')

Constructs a FilterProcessResult object.

string
getProcessedText()

Gets the processed text.

string
__toString()

Gets the processed text.

$this
setProcessedText(string $processed_text)

Sets the processed text.

string
createPlaceholder(string $callback, array $args)

Creates a placeholder.

Details

protected $this setCacheability(CacheableDependencyInterface $cacheability)

Sets cacheability; useful for value object constructors.

Parameters

CacheableDependencyInterface $cacheability

The cacheability to set.

Return Value

$this

getCacheTags()

{@inheritdoc}

getCacheContexts()

{@inheritdoc}

getCacheMaxAge()

{@inheritdoc}

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)

Creates a new bubbleable metadata object by merging this one with another.

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 bubbleable metadata object to a render array.

Parameters

array $build

A render array.

static CacheableMetadata createFromRenderArray(array $build)

Creates a bubbleable metadata 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 bubbleable metadata 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

getAttachments()

{@inheritdoc}

addAttachments(array $attachments)

{@inheritdoc}

Parameters

array $attachments

setAttachments(array $attachments)

{@inheritdoc}

Parameters

array $attachments

static array mergeAttachments(array $a, array $b)

Merges two attachments arrays (which live under the '#attached' key).

The values under the 'drupalSettings' key are merged in a special way, to match the behavior of:

Parameters

array $a

An attachments array.

array $b

Another attachments array.

Return Value

array

The merged attachments array.

__construct(string $processed_text = '')

Constructs a FilterProcessResult object.

Parameters

string $processed_text

The text as processed by a text filter.

string getProcessedText()

Gets the processed text.

Return Value

string

string __toString()

Gets the processed text.

Return Value

string

$this setProcessedText(string $processed_text)

Sets the processed text.

Parameters

string $processed_text

The text as processed by a text filter.

Return Value

$this

string createPlaceholder(string $callback, array $args)

Creates a placeholder.

This generates its own placeholder markup for one major reason: to not have FilterProcessResult depend on the Renderer service, because this is a value object. As a side-effect and added benefit, this makes it easier to distinguish placeholders for filtered text versus generic render system placeholders.

Parameters

string $callback

The #lazy_builder callback that will replace the placeholder with its eventual markup.

array $args

The arguments for the #lazy_builder callback.

Return Value

string

The placeholder markup.