diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index f2b83e8..7951a54 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -53,7 +53,7 @@ public function merge(CacheableMetadata $other) { $result->attached = $this->attached; } else { - $result->attached = \Drupal::service('renderer')->mergeAttachments($this->attached, $other->attached); + $result->attached = static::mergeAttachments($this->attached, $other->attached); } if (empty($this->postRenderCache)) { @@ -171,4 +171,65 @@ public function setPostRenderCacheCallbacks(array $post_render_cache_callbacks) return $this; } + /** + * 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: + * + * @code + * jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...) + * @endcode + * + * This means integer indices are preserved just like string indices are, + * rather than re-indexed as is common in PHP array merging. + * + * Example: + * @code + * function module1_page_attachments(&$page) { + * $page['a']['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c']; + * } + * function module2_page_attachments(&$page) { + * $page['#attached']['drupalSettings']['foo'] = ['d']; + * } + * // When the page is rendered after the above code, and the browser runs the + * // resulting