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 BubbleableMetadata();
$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
Issue fork drupal-3280235
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
- 3280235-calling-code-can
changes, plain diff MR !12689
Comments
Comment #7
smustgrave commentedAdded the suggestion but still NW for the test coverage which I'm not entirely sure would look like
Comment #9
smustgrave commentedSo wasn't sure how to write a test for this so I did ask Claude code (acknowledging use of AI). I did cleanup the output some as it was adding test coverage for AttachmentsInterface specifically and this is more about making sure TextProcessed works with AttachmentsInterface.
Comment #10
dcam commentedOverall it looks OK to me. If the issue is that we can't get attachments from
TextProcessedobjects, then the test shows that we can with this change.I left one suggestion on the MR to fix an implicit
any()expectation on the mocked object.Comment #11
smustgrave commentedApplied the suggestion.
Comment #12
dcam commentedLooks good to me.
Comment #13
godotislateCouple things:
First, this example in the IS:
Since
$cacheis an instance of CacheableMetadata, and not BubbleableMetdata, it can't capture attachments anyway. So I assume this should be:Second, with the changes in the MR, the attachments in the TextProcessed
processedproperty of aTextItemfield item can be captured, but the IS mentions that the attachments in the FilterProcessResultprocessedproperty of theTextProcesseddata type are not captured. (Yes, this is probably a little confusing.) The MR changes AFAICT do not capture these. In order to do so, I thinkgetAttachments()needs to be overridde in TextProcessed so that it's something like:And test coverage for this as well.
Comment #15
a.dmitriiev commentedSimilar problem was reported here #3584177: highlight js doenst highlight code in Drupal cms 2.x D11, so I am raising the priority to "Normal" as it seems it affects all sites that use canvas. In the referenced issue the same fix was suggested. I have also added a kernel test with FilterProcessResult.
Comment #16
smustgrave commentedFeedback looks good to me.
Comment #21
godotislateCommitted and pushed b5b29d4 to main, 3460ecf to 11.x, and f05f7ab to 11.4.x. Thanks!