attachments)) { $result->attachments = $other->attachments; } elseif (empty($other->attachments)) { $result->attachments = $this->attachments; } else { $result->attachments = static::mergeAttachments($this->attachments, $other->attachments); } } return $result; } /** * Applies the values of this bubbleable metadata object to a render array. * * @param array &$build * A render array. */ public function applyTo(array &$build) { parent::applyTo($build); $build['#attached'] = $this->attachments; } /** * Creates a bubbleable metadata object with values taken from a render array. * * @param array $build * A render array. * * @return static */ public static function createFromRenderArray(array $build) { $meta = parent::createFromRenderArray($build); $meta->attachments = (isset($build['#attached'])) ? $build['#attached'] : []; return $meta; } /** * Creates a bubbleable metadata object from a depended object. * * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object * The object whose cacheability metadata to retrieve. If it implements * CacheableDependencyInterface, its cacheability metadata will be used, * otherwise, the passed in object must be assumed to be uncacheable, so * max-age 0 is set. * * @return static */ public static function createFromObject($object) { $meta = parent::createFromObject($object); if ($object instanceof AttachmentsInterface) { $meta->attachments = $object->getAttachments(); } return $meta; } /** * {@inheritdoc} */ public function addCacheableDependency($other_object) { parent::addCacheableDependency($other_object); if ($other_object instanceof AttachmentsInterface) { $this->addAttachments($other_object->getAttachments()); } 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