interface AttachmentsInterface

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/AttachmentsInterface.php \Drupal\Core\Render\AttachmentsInterface
  2. 8.9.x core/lib/Drupal/Core/Render/AttachmentsInterface.php \Drupal\Core\Render\AttachmentsInterface
  3. 10 core/lib/Drupal/Core/Render/AttachmentsInterface.php \Drupal\Core\Render\AttachmentsInterface

The attached metadata collection for a renderable element.

Libraries, JavaScript settings, feeds, HTML <head> tags, HTML <head> links, HTTP headers, and the HTTP status code are attached to render arrays using the #attached property. The #attached property is an associative array, where the keys are the attachment types and the values are the attached data. For example:

$build['#attached']['library'][] = 'core/jquery';
$build['#attached']['http_header'] = [
    [
        'Content-Type',
        'application/rss+xml; charset=utf-8',
    ],
];

The keys used by core are:

  • drupalSettings: (optional) JavaScript settings.
  • feed: (optional) RSS feeds.
  • html_head: (optional) Tags used in HTML <head>.
  • html_head_link: (optional) The <link> tags in HTML <head>.
  • http_header: (optional) HTTP headers and status code.
  • html_response_attachment_placeholders: (optional) Placeholders used in a response attachment
  • library: (optional) Asset libraries.
  • placeholders: (optional) Any placeholders.

@todo If in Drupal 9, we remove attachments other than assets (libraries + drupalSettings), then we can look into unifying this with \Drupal\Core\Asset\AttachedAssetsInterface.

Hierarchy

Expanded class hierarchy of AttachmentsInterface

All classes that implement AttachmentsInterface

See also

\Drupal\Core\Render\AttachmentsTrait

10 files declare their use of AttachmentsInterface
AjaxResponse.php in core/lib/Drupal/Core/Ajax/AjaxResponse.php
AjaxResponseAttachmentsProcessor.php in core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
AttachmentsTestDomainObject.php in core/modules/system/tests/modules/early_rendering_controller_test/src/AttachmentsTestDomainObject.php
AttachmentsTestResponse.php in core/modules/system/tests/modules/early_rendering_controller_test/src/AttachmentsTestResponse.php
BigPipeResponseAttachmentsProcessor.php in core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php

... See full list

File

core/lib/Drupal/Core/Render/AttachmentsInterface.php, line 38

Namespace

Drupal\Core\Render
View source
interface AttachmentsInterface {
    
    /**
     * Gets this object's attached collection.
     *
     * @return array
     *   The attachments array.
     */
    public function getAttachments();
    
    /**
     * Merges an array of attached data into this object's collection.
     *
     * @param array $attachments
     *   The attachments to add.
     *
     * @return $this
     */
    public function addAttachments(array $attachments);
    
    /**
     * Replaces this object's attached data with the provided array.
     *
     * @param array $attachments
     *   The attachments to set.
     *
     * @return $this
     */
    public function setAttachments(array $attachments);

}

Members

Title Sort descending Modifiers Object type Summary
AttachmentsInterface::addAttachments public function Merges an array of attached data into this object&#039;s collection.
AttachmentsInterface::getAttachments public function Gets this object&#039;s attached collection.
AttachmentsInterface::setAttachments public function Replaces this object&#039;s attached data with the provided array.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.