3 namespace Drupal\Core\Render;
5 use Drupal\Component\Utility\NestedArray;
6 use Drupal\Core\Cache\CacheableMetadata;
9 * Value object used for bubbleable rendering metadata.
11 * @see \Drupal\Core\Render\RendererInterface::render()
13 class BubbleableMetadata extends CacheableMetadata implements AttachmentsInterface {
18 * Merges the values of another bubbleable metadata object with this one.
20 * @param \Drupal\Core\Cache\CacheableMetadata $other
21 * The other bubbleable metadata object.
24 * A new bubbleable metadata object, with the merged data.
26 public function merge(CacheableMetadata $other) {
27 $result = parent::merge($other);
29 // This is called many times per request, so avoid merging unless absolutely
31 if ($other instanceof BubbleableMetadata) {
32 if (empty($this->attachments)) {
33 $result->attachments = $other->attachments;
35 elseif (empty($other->attachments)) {
36 $result->attachments = $this->attachments;
39 $result->attachments = static::mergeAttachments($this->attachments, $other->attachments);
47 * Applies the values of this bubbleable metadata object to a render array.
49 * @param array &$build
52 public function applyTo(array &$build) {
53 parent::applyTo($build);
54 $build['#attached'] = $this->attachments;
58 * Creates a bubbleable metadata object with values taken from a render array.
65 public static function createFromRenderArray(array $build) {
66 $meta = parent::createFromRenderArray($build);
67 $meta->attachments = (isset($build['#attached'])) ? $build['#attached'] : [];
72 * Creates a bubbleable metadata object from a depended object.
74 * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object
75 * The object whose cacheability metadata to retrieve. If it implements
76 * CacheableDependencyInterface, its cacheability metadata will be used,
77 * otherwise, the passed in object must be assumed to be uncacheable, so
82 public static function createFromObject($object) {
83 $meta = parent::createFromObject($object);
85 if ($object instanceof AttachmentsInterface) {
86 $meta->attachments = $object->getAttachments();
95 public function addCacheableDependency($other_object) {
96 parent::addCacheableDependency($other_object);
98 if ($other_object instanceof AttachmentsInterface) {
99 $this->addAttachments($other_object->getAttachments());
106 * Merges two attachments arrays (which live under the '#attached' key).
108 * The values under the 'drupalSettings' key are merged in a special way, to
109 * match the behavior of:
112 * jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...)
115 * This means integer indices are preserved just like string indices are,
116 * rather than re-indexed as is common in PHP array merging.
120 * function module1_page_attachments(&$page) {
121 * $page['a']['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c'];
123 * function module2_page_attachments(&$page) {
124 * $page['#attached']['drupalSettings']['foo'] = ['d'];
126 * // When the page is rendered after the above code, and the browser runs the
127 * // resulting <SCRIPT> tags, the value of drupalSettings.foo is
128 * // ['d', 'b', 'c'], not ['a', 'b', 'c', 'd'].
131 * By following jQuery.extend() merge logic rather than common PHP array merge
132 * logic, the following are ensured:
133 * - Attaching JavaScript settings is idempotent: attaching the same settings
134 * twice does not change the output sent to the browser.
135 * - If pieces of the page are rendered in separate PHP requests and the
136 * returned settings are merged by JavaScript, the resulting settings are
137 * the same as if rendered in one PHP request and merged by PHP.
140 * An attachments array.
142 * Another attachments array.
145 * The merged attachments array.
147 public static function mergeAttachments(array $a, array $b) {
148 // If both #attached arrays contain drupalSettings, then merge them
149 // correctly; adding the same settings multiple times needs to behave
151 if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) {
152 $drupalSettings = NestedArray::mergeDeepArray([$a['drupalSettings'], $b['drupalSettings']], TRUE);
153 // No need for re-merging them.
154 unset($a['drupalSettings']);
155 unset($b['drupalSettings']);
157 // Optimize merging of placeholders: no need for deep merging.
158 if (!empty($a['placeholders']) && !empty($b['placeholders'])) {
159 $placeholders = $a['placeholders'] + $b['placeholders'];
160 // No need for re-merging them.
161 unset($a['placeholders']);
162 unset($b['placeholders']);
164 // Apply the normal merge.
165 $a = array_merge_recursive($a, $b);
166 if (isset($drupalSettings)) {
167 // Save the custom merge for the drupalSettings.
168 $a['drupalSettings'] = $drupalSettings;
170 if (isset($placeholders)) {
171 // Save the custom merge for the placeholders.
172 $a['placeholders'] = $placeholders;