diff --git a/core/includes/common.inc b/core/includes/common.inc index 27c9c54..2731dc1 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -20,6 +20,7 @@ use Drupal\Component\Utility\String; use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\UrlHelper; +use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Cache\Cache; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Site\Settings; @@ -889,15 +890,12 @@ function drupal_js_defaults($data = NULL) { * * @return array * The merged #attached array. + * + * @deprecated To be removed in Drupal 8.0.x. Use + * \Drupal\Core\Asset\AttachedAssets::mergeAttachments() instead. */ function drupal_merge_attached(array $a, array $b) { - // If both #attached arrays contain drupalSettings, then merge them correctly; - // adding the same settings multiple times needs to behave idempotently. - if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) { - $a['drupalSettings'] = NestedArray::mergeDeepArray([$a['drupalSettings'], $b['drupalSettings']], TRUE); - unset($b['drupalSettings']); - } - return NestedArray::mergeDeep($a, $b); + return AttachedAssets::mergeAttachments($a, $b); } /** diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php index 343c813..766026d 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponse.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php @@ -77,7 +77,7 @@ public function addCommand(CommandInterface $command, $prepend = FALSE) { 'library' => $assets->getLibraries(), 'drupalSettings' => $assets->getSettings(), ]; - $attachments = drupal_merge_attached($this->attachments, $attachments); + $attachments = AttachedAssets::mergeAttachments($this->attachments, $attachments); $this->setAttachments($attachments); } diff --git a/core/lib/Drupal/Core/Asset/AttachedAssets.php b/core/lib/Drupal/Core/Asset/AttachedAssets.php index d94618e..f0ef5ec 100644 --- a/core/lib/Drupal/Core/Asset/AttachedAssets.php +++ b/core/lib/Drupal/Core/Asset/AttachedAssets.php @@ -5,6 +5,7 @@ */ namespace Drupal\Core\Asset; +use Drupal\Component\Utility\NestedArray; /** * The default attached assets collection. @@ -51,6 +52,59 @@ public static function createFromRenderArray(array $render_array) { } /** + * Merges two #attached arrays. + * + * 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