mergeAttachments example

$expected['#attached'] = [
      'library' => [
        'core/drupal',
        'core/drupalSettings',
        'core/jquery',
      ],
      'drupalSettings' => [
        'foo' => ['d'],
        'bar' => ['a', 'b', 'c'],
      ],
    ];
    $this->assertSame($expected['#attached'], BubbleableMetadata::mergeAttachments($a['#attached']$b['#attached']), 'Attachments merged correctly.');

    // Merging in the opposite direction yields the opposite library order.     $expected['#attached'] = [
      'library' => [
        'core/jquery',
        'core/drupal',
        'core/drupalSettings',
      ],
      'drupalSettings' => [
        'bar' => ['a', 'b', 'c'],
        'foo' => ['d'],
      ],
// This is called many times per request, so avoid merging unless absolutely     // necessary.     if ($other instanceof BubbleableMetadata) {
      if (empty($this->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. */

class AttachedRenderingBlock extends BlockBase {

  /** * {@inheritdoc} */
  public function build() {
    // Grab test attachment fixtures from     // Drupal\render_attached_test\Controller\RenderAttachedTestController.     $controller = new RenderAttachedTestController();
    $attached = BubbleableMetadata::mergeAttachments($controller->feed()$controller->head());
    $attached = BubbleableMetadata::mergeAttachments($attached$controller->header());
    $attached = BubbleableMetadata::mergeAttachments($attached$controller->teapotHeaderStatus());

    // Return some arbitrary markup so the block doesn't disappear.     $attached['#markup'] = 'Markup from attached_rendering_block.';
    return $attached;
  }

  /** * {@inheritdoc} */
  
      // list the final, processed attachments.       $attached['library'] = $assets->getLibraries();
      $attached['drupalSettings'] = $assets->getSettings();

      // Since we can only replace content in the HTML head section if there's a       // placeholder for it, we can safely avoid processing the render array if       // it's not present.       if (!empty($attachment_placeholders['head'])) {
        // 'feed' is a special case of 'html_head_link'. We process them into         // 'html_head_link' entries and merge them.         if (!empty($attached['feed'])) {
          $attached = BubbleableMetadata::mergeAttachments(
            $attached,
            $this->processFeed($attached['feed'])
          );
          unset($attached['feed']);
        }
        // 'html_head_link' is a special case of 'html_head' which can be present         // as a head element, but also as a Link: HTTP header depending on         // settings in the render array. Processing it can add to both the         // 'html_head' and 'http_header' keys of '#attached', so we must address         // it before 'html_head'.         if (!empty($attached['html_head_link'])) {
          
// Hide the text format's filters' guidelines of those text formats that have     // a text editor associated: they're rather useless when using a text editor.     foreach ($editors as $format_id => $editor) {
      $element['format']['guidelines'][$format_id]['#access'] = FALSE;
    }

    // Attach Text Editor module's (this module) library.     $element['#attached']['library'][] = 'editor/drupal.editor';

    // Attach attachments for all available editors.     $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached']$this->pluginManager->getAttachments($format_ids));

    // Apply XSS filters when editing content if necessary. Some types of text     // editors cannot guarantee that the end user won't become a victim of XSS.     if (!empty($element['value']['#value'])) {
      $original = $element['value']['#value'];
      $format = FilterFormat::load($element['format']['format']['#value']);

      // Ensure XSS-safety for the current text format/editor.       $filtered = editor_filter_xss($original$format);
      if ($filtered !== FALSE) {
        $element['value']['#value'] = $filtered;
      }
array_unshift($this->commands, $command->render());
    }
    else {
      $this->commands[] = $command->render();
    }
    if ($command instanceof CommandWithAttachedAssetsInterface) {
      $assets = $command->getAttachedAssets();
      $attachments = [
        'library' => $assets->getLibraries(),
        'drupalSettings' => $assets->getSettings(),
      ];
      $attachments = BubbleableMetadata::mergeAttachments($this->getAttachments()$attachments);
      $this->setAttachments($attachments);
    }

    return $this;
  }

  /** * Gets all AJAX commands. * * @return array * Returns render arrays for all previously added commands. */
/** * {@inheritdoc} */
  public function getAttachments() {
    return $this->attachments;
  }

  /** * {@inheritdoc} */
  public function addAttachments(array $attachments) {
    $this->attachments = BubbleableMetadata::mergeAttachments($this->attachments, $attachments);
    return $this;
  }

  /** * {@inheritdoc} */
  public function setAttachments(array $attachments) {
    $this->attachments = $attachments;
    return $this;
  }

}
$child = &$element[$key];
      // If the child has links which have not been printed yet and the user has       // access to it, merge its links in to the parent.       if (isset($child['#links']) && empty($child['#printed']) && Element::isVisibleElement($child)) {
        $element['#links'] += $child['#links'];
        // Mark the child as having been printed already (so that its links         // cannot be mistakenly rendered twice).         $child['#printed'] = TRUE;
      }
      // Merge attachments.       if (isset($child['#attached'])) {
        $element['#attached'] = BubbleableMetadata::mergeAttachments($element['#attached']$child['#attached']);
      }
    }
    return $element;
  }

}
Home | Imprint | This part of the site doesn't use cookies.