Chris@0: attachments)) { Chris@0: $result->attachments = $other->attachments; Chris@0: } Chris@0: elseif (empty($other->attachments)) { Chris@0: $result->attachments = $this->attachments; Chris@0: } Chris@0: else { Chris@0: $result->attachments = static::mergeAttachments($this->attachments, $other->attachments); Chris@0: } Chris@0: } Chris@0: Chris@0: return $result; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Applies the values of this bubbleable metadata object to a render array. Chris@0: * Chris@0: * @param array &$build Chris@0: * A render array. Chris@0: */ Chris@0: public function applyTo(array &$build) { Chris@0: parent::applyTo($build); Chris@0: $build['#attached'] = $this->attachments; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a bubbleable metadata object with values taken from a render array. Chris@0: * Chris@0: * @param array $build Chris@0: * A render array. Chris@0: * Chris@0: * @return static Chris@0: */ Chris@0: public static function createFromRenderArray(array $build) { Chris@0: $meta = parent::createFromRenderArray($build); Chris@0: $meta->attachments = (isset($build['#attached'])) ? $build['#attached'] : []; Chris@0: return $meta; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a bubbleable metadata object from a depended object. Chris@0: * Chris@0: * @param \Drupal\Core\Cache\CacheableDependencyInterface|mixed $object Chris@0: * The object whose cacheability metadata to retrieve. If it implements Chris@0: * CacheableDependencyInterface, its cacheability metadata will be used, Chris@0: * otherwise, the passed in object must be assumed to be uncacheable, so Chris@0: * max-age 0 is set. Chris@0: * Chris@0: * @return static Chris@0: */ Chris@0: public static function createFromObject($object) { Chris@0: $meta = parent::createFromObject($object); Chris@0: Chris@0: if ($object instanceof AttachmentsInterface) { Chris@0: $meta->attachments = $object->getAttachments(); Chris@0: } Chris@0: Chris@0: return $meta; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function addCacheableDependency($other_object) { Chris@0: parent::addCacheableDependency($other_object); Chris@0: Chris@0: if ($other_object instanceof AttachmentsInterface) { Chris@0: $this->addAttachments($other_object->getAttachments()); Chris@0: } Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges two attachments arrays (which live under the '#attached' key). Chris@0: * Chris@0: * The values under the 'drupalSettings' key are merged in a special way, to Chris@0: * match the behavior of: Chris@0: * Chris@0: * @code Chris@0: * jQuery.extend(true, {}, $settings_items[0], $settings_items[1], ...) Chris@0: * @endcode Chris@0: * Chris@0: * This means integer indices are preserved just like string indices are, Chris@0: * rather than re-indexed as is common in PHP array merging. Chris@0: * Chris@0: * Example: Chris@0: * @code Chris@0: * function module1_page_attachments(&$page) { Chris@0: * $page['a']['#attached']['drupalSettings']['foo'] = ['a', 'b', 'c']; Chris@0: * } Chris@0: * function module2_page_attachments(&$page) { Chris@0: * $page['#attached']['drupalSettings']['foo'] = ['d']; Chris@0: * } Chris@0: * // When the page is rendered after the above code, and the browser runs the Chris@0: * // resulting