\Drupal\Core\Render\BubbleableMetadata::applyTo overwrites "#attached" key on provided build array

Created on 5 August 2021, almost 4 years ago
Updated 12 February 2023, over 2 years ago

Problem/Motivation

Using \Drupal\Core\Render\BubbleableMetadata::applyTo on a build array that already has content is the #attached key causes that key to be overwritten. This appears to be intentional behavior to specific to \Drupal\Core\Render\BubbleableMetadata::applyTo which overrides \Drupal\Core\Cache\CacheableMetadata::applyTo but its unclear why this is done (i.e. I'm not sure if this is a bug or confusion about how to use this functionality).

Steps to reproduce

Example code below is a simplified version of a custom block where I ran in to this issue. The theme for this block has an additional "url" parameter where the code provides a URL and needs to attached it's metadata to the build array.

namespace Drupal\custom_module\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;

/**
 * Provides a custom block with attachments.
 *
 * @Block(
 *   id = "custom_module_custom_block",
 *   admin_label = @Translation("Custom Module: Custom Block"),
 * )
 */
class CustomBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $build = [
      '#theme' => 'custom_block_theme',
      '#url' => NULL,
      '#attached' => ['library' => ['custom_theme/custom_library']],
    ];
    $url = Url::fromRoute('custom_module.custom_route');
    $url_metadata = $url->toString(TRUE);
    $build['#url'] = $url_metadata->getGeneratedUrl();
    $url_metadata->applyTo($build);
    // After above -- $build['#attached'] is now empty.
    return $build;
  }

}

Proposed resolution

Merge existing attached from passed in build array with $this->attachments.

Remaining tasks

Create patch. I'm unsure of the best approach to take here and/or if this should even be done so further discussion and a patch is needed.

User interface changes

None.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
CacheΒ  β†’

Last updated 12 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States wells Seattle, WA

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.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    This issue is being reviewed by the kind folks in Slack, #needs-review-queue-initiative. We are working to keep the size of Needs Review queue [2700+ issues] to around 400 (1 month or less), following Review a patch or merge request β†’ as a guide.

    Did not test MR

    Can it be updated for 10.1 please

  • Status changed to Needs review over 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States wells Seattle, WA

    For some reason I can't create a branch against 10.1.x. Trying a patch file instead...

    Changes in this patch are the same as those MR !1017 as of e91772db.

  • Status changed to Needs work over 2 years ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Seems there were some failures in #9

    For some reason I can't create a branch against 10.1.x. Trying a patch file instead...

    Yea something to do when the MR was created before 10.1.x existed I think. Super pain

  • πŸ‡ΊπŸ‡ΈUnited States wells Seattle, WA

    Thanks for flagging. Weird number of fails in there that don't look related at all... But this one is definitely relevant: Drupal_Tests_Core_Render_RendererBubblingTest

Production build 0.71.5 2024