Calling code can not collect bubbled attachments from TextProcessed

Created on 12 May 2022, about 3 years ago
Updated 10 July 2025, 5 days ago

Problem/Motivation

TextItem contains a processed property
This property is a computed field built with the TextProcessed class
Calling $entity->text_field->processed will return you the filtered text, i.e. with all filters applied.

At this point if you're using $entity->text_field->processed in a render array, you should also take care to collect its cacheable-metadata. Because TextProcessed implements CacheableDependencyInterface, you can do that like so

$cache = new CacheableMetadata();
$a_render_array = [
  '#theme' => 'biscuits',
  '#cookies' => $entity->text_field->processed,
];
$cache->addCacheableDependency($entity->text_field->processed);
$cache->applyTo($a_render_array);

However a the processed protected property on TextProcessed is an instance of FilterProcessResult, which extends from BubbleableMetadata, not just CacheableMetadata

This means there are #attachments inside it too, not just #cache entries.

There is no way to access those attachments and add them to your render array.

This is minor because you can work around this by doing this instead

$a_render_array = [
  '#theme' => 'biscuits',
  '#cookies' => [
    '#type' => 'processed_text',
    '#text' => $entity->text_field->value,
    '#format' => $entity->text_field->format,
];

Steps to reproduce

Write a filter plugin that adds Javascript or CSS (or any #attached)
Add an input format with that filter plugin enabled
Create an entity with a text field on it
Access the contents of the field for use in a render array using $entity->text_field->processed
Note that your render array won't include the attachments

Proposed resolution

Have TextProcessed also implement AttachmentsInterface

Remaining tasks

Implement the interface
Add a test

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component

text.module

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024