BubbleableMetadata::mergeAttachments

public static BubbleableMetadata::mergeAttachments(array $a, array $b)

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:

  jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...)

This means integer indices are preserved just like string indices are, rather than re-indexed as is common in PHP array merging.

Example:

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 <SCRIPT> tags, the value of drupalSettings.foo is
// ['d', 'b', 'c'], not ['a', 'b', 'c', 'd'].

By following jQuery.extend() merge logic rather than common PHP array merge logic, the following are ensured:

  • Attaching JavaScript settings is idempotent: attaching the same settings twice does not change the output sent to the browser.
  • If pieces of the page are rendered in separate PHP requests and the returned settings are merged by JavaScript, the resulting settings are the same as if rendered in one PHP request and merged by PHP.

Parameters

array $a: An attachments array.

array $b: Another attachments array.

Return value

array The merged attachments array.

File

core/lib/Drupal/Core/Render/BubbleableMetadata.php, line 147

Class

BubbleableMetadata
Value object used for bubbleable rendering metadata.

Namespace

Drupal\Core\Render

Code

public static function mergeAttachments(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'])) {
    $drupalSettings = NestedArray::mergeDeepArray(array($a['drupalSettings'], $b['drupalSettings']), TRUE);
    // No need for re-merging them.
    unset($a['drupalSettings']);
    unset($b['drupalSettings']);
  }
  // Optimize merging of placeholders: no need for deep merging.
  if (!empty($a['placeholders']) && !empty($b['placeholders'])) {
    $placeholders = $a['placeholders'] + $b['placeholders'];
    // No need for re-merging them.
    unset($a['placeholders']);
    unset($b['placeholders']);
  }
  // Apply the normal merge.
  $a = array_merge_recursive($a, $b);
  if (isset($drupalSettings)) {
    // Save the custom merge for the drupalSettings.
    $a['drupalSettings'] = $drupalSettings;
  }
  if (isset($placeholders)) {
    // Save the custom merge for the placeholders.
    $a['placeholders'] = $placeholders;
  }
  return $a;
}
doc_Drupal
2016-10-29 08:48:59
Comments
Leave a Comment

Please login to continue.