\Drupal\Core\Render\BubbleableMetadata::applyTo does not consider that the render array may already have attachments

Created on 11 January 2023, over 2 years ago
Updated 18 August 2025, 7 days ago

Problem/Motivation

\Drupal\Core\Render\BubbleableMetadata::applyTo overwrites the attachments property on a render array, rather than merging with any existing attachments

Steps to reproduce

Create a render array, and set #attachments property on it.
Create a BubbleableMetadata object, and call ::applyTo with the render array
See your existing attachments clobbered

Proposed resolution

Merge instead of wipe like we do for #cache

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Closed: works as designed

Version

11.0 πŸ”₯

Component

render system

Created by

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

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

Comments & Activities

Not all content is available!

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

  • πŸ‡¦πŸ‡ΊAustralia acbramley

    This is the same as CacheableMetadata::applyTo where you should merge metadata before using applyTo.

    Something like:

    $x = new BubbleableMetadata();
    // Add stuff to $x
    $y = BubbleableMetadata::createFromRenderArray($build);
    $x = $x->merge($y); // or use $x->addCacheableDependency($y);
    $x->applyTo($build);
    
Production build 0.71.5 2024