core/lib/Drupal/Core/Ajax/AjaxResponse.php | 4 +-- .../Core/Ajax/AjaxResponseAttachmentsProcessor.php | 4 +-- ...ponseInterface.php => AttachmentsInterface.php} | 28 ++++++++++++++------- .../AttachmentsResponseProcessorInterface.php | 6 ++--- .../Core/Render/AttachmentsResponseTrait.php | 29 +++++++++++----------- core/lib/Drupal/Core/Render/BubbleableMetadata.php | 21 +++------------- core/lib/Drupal/Core/Render/HtmlResponse.php | 4 +-- .../Render/HtmlResponseAttachmentsProcessor.php | 2 +- 8 files changed, 48 insertions(+), 50 deletions(-) diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponse.php b/core/lib/Drupal/Core/Ajax/AjaxResponse.php index 5f6f0d5..20395ea 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponse.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponse.php @@ -10,7 +10,7 @@ use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Render\BubbleableMetadata; use Drupal\Core\Render\Renderer; -use Drupal\Core\Render\AttachmentsResponseInterface; +use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\AttachmentsResponseTrait; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; @@ -22,7 +22,7 @@ * * @ingroup ajax */ -class AjaxResponse extends JsonResponse implements AttachmentsResponseInterface { +class AjaxResponse extends JsonResponse implements AttachmentsInterface { use AttachmentsResponseTrait; diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php index d1e456b..a0ebe4c 100644 --- a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php +++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php @@ -11,7 +11,7 @@ use Drupal\Core\Asset\AttachedAssets; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Extension\ModuleHandlerInterface; -use Drupal\Core\Render\AttachmentsResponseInterface; +use Drupal\Core\Render\AttachmentsInterface; use Drupal\Core\Render\AttachmentsResponseProcessorInterface; use Drupal\Core\Render\RendererInterface; use Symfony\Component\HttpFoundation\Request; @@ -105,7 +105,7 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor /** * {@inheritdoc} */ - public function processAttachments(AttachmentsResponseInterface $response) { + public function processAttachments(AttachmentsInterface $response) { // @todo Convert to assertion once https://www.drupal.org/node/2408013 lands if (!$response instanceof AjaxResponse) { throw new \InvalidArgumentException('\Drupal\Core\Ajax\AjaxResponse instance expected.'); diff --git a/core/lib/Drupal/Core/Render/AttachmentsResponseInterface.php b/core/lib/Drupal/Core/Render/AttachmentsInterface.php similarity index 59% rename from core/lib/Drupal/Core/Render/AttachmentsResponseInterface.php rename to core/lib/Drupal/Core/Render/AttachmentsInterface.php index fd501f9..39a9282 100644 --- a/core/lib/Drupal/Core/Render/AttachmentsResponseInterface.php +++ b/core/lib/Drupal/Core/Render/AttachmentsInterface.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\Render\AttachmentsResponseInterface. + * Contains \Drupal\Core\Render\AttachmentsInterface. */ namespace Drupal\Core\Render; @@ -16,24 +16,34 @@ * * @see \Drupal\Core\Render\AttachmentsResponseTrait */ -interface AttachmentsResponseInterface { +interface AttachmentsInterface { /** - * Sets attachments for this response. + * Gets attachments. + * + * @return array + * The attachments. + */ + public function getAttachments(); + + /** + * Adds attachments. * * @param array $attachments - * An #attached array. + * The attachments to add. * * @return $this */ - public function setAttachments(array $attachments); + public function addAttachments(array $attachments); /** - * Gets attachments set for this response. + * Sets attachments. * - * @return array $attachments - * An #attached array. + * @param array $attachments + * The attachments to set. + * + * @return $this */ - public function getAttachments(); + public function setAttachments(array $attachments); } diff --git a/core/lib/Drupal/Core/Render/AttachmentsResponseProcessorInterface.php b/core/lib/Drupal/Core/Render/AttachmentsResponseProcessorInterface.php index cc504de..8257763 100644 --- a/core/lib/Drupal/Core/Render/AttachmentsResponseProcessorInterface.php +++ b/core/lib/Drupal/Core/Render/AttachmentsResponseProcessorInterface.php @@ -17,14 +17,14 @@ /** * Processes the attachments of a response that has attachments. * - * @param \Drupal\Core\Render\AttachmentsResponseInterface $response + * @param \Drupal\Core\Render\AttachmentsInterface $response * The response to process the attachments for. * - * @return \Drupal\Core\Render\AttachmentsResponseInterface + * @return \Drupal\Core\Render\AttachmentsInterface * The processed response. * * @throws \InvalidArgumentException */ - public function processAttachments(AttachmentsResponseInterface $response); + public function processAttachments(AttachmentsInterface $response); } diff --git a/core/lib/Drupal/Core/Render/AttachmentsResponseTrait.php b/core/lib/Drupal/Core/Render/AttachmentsResponseTrait.php index 732aa1a..daece91 100644 --- a/core/lib/Drupal/Core/Render/AttachmentsResponseTrait.php +++ b/core/lib/Drupal/Core/Render/AttachmentsResponseTrait.php @@ -3,9 +3,9 @@ namespace Drupal\Core\Render; /** - * Provides an implementation of AttachmentsResponseInterface. + * Provides an implementation of AttachmentsInterface. * - * @see \Drupal\Core\Render\AttachmentsResponseInterface + * @see \Drupal\Core\Render\AttachmentsInterface */ trait AttachmentsResponseTrait { @@ -14,28 +14,29 @@ * * @var array */ - protected $attachments = [ - 'library' => [], - 'drupalSettings' => [], - 'html_head' => [], - 'feed' => [], - 'html_head_link' => [], - 'http_header' => [], - ]; + protected $attachments = []; /** * {@inheritdoc} */ - public function setAttachments(array $attachments) { - $this->attachments = $attachments; + public function getAttachments() { + return $this->attachments; + } + + /** + * {@inheritdoc} + */ + public function addAttachments(array $attachments) { + $this->attachments = BubbleableMetadata::mergeAttachments($this->attachments, $attachments); return $this; } /** * {@inheritdoc} */ - public function getAttachments() { - return $this->attachments; + public function setAttachments(array $attachments) { + $this->attachments = $attachments; + return $this; } } diff --git a/core/lib/Drupal/Core/Render/BubbleableMetadata.php b/core/lib/Drupal/Core/Render/BubbleableMetadata.php index 0e41072..9b166f6 100644 --- a/core/lib/Drupal/Core/Render/BubbleableMetadata.php +++ b/core/lib/Drupal/Core/Render/BubbleableMetadata.php @@ -15,7 +15,7 @@ * * @see \Drupal\Core\Render\RendererInterface::render() */ -class BubbleableMetadata extends CacheableMetadata { +class BubbleableMetadata extends CacheableMetadata implements AttachmentsInterface { /** * Attached assets. @@ -79,22 +79,14 @@ public static function createFromRenderArray(array $build) { } /** - * Gets attachments. - * - * @return array - * The attachments + * {@inheritdoc} */ public function getAttachments() { return $this->attached; } /** - * Adds attachments. - * - * @param array $attachments - * The attachments to add. - * - * @return $this + * {@inheritdoc} */ public function addAttachments(array $attachments) { $this->attached = \Drupal::service('renderer')->mergeAttachments($this->attached, $attachments); @@ -102,12 +94,7 @@ public function addAttachments(array $attachments) { } /** - * Sets attachments. - * - * @param array $attachments - * The attachments to set. - * - * @return $this + * {@inheritdoc} */ public function setAttachments(array $attachments) { $this->attached = $attachments; diff --git a/core/lib/Drupal/Core/Render/HtmlResponse.php b/core/lib/Drupal/Core/Render/HtmlResponse.php index b8107b0..2bf88f0 100644 --- a/core/lib/Drupal/Core/Render/HtmlResponse.php +++ b/core/lib/Drupal/Core/Render/HtmlResponse.php @@ -21,10 +21,10 @@ * Supports Drupal's idea of #attached metadata: libraries, settings, http_header and html_head. * * @see \Drupal\Core\Cache\CacheableResponse - * @see \Drupal\Core\Render\AttachmentsResponseInterface + * @see \Drupal\Core\Render\AttachmentsInterface * @see \Drupal\Core\Render\AttachmentsResponseTrait */ -class HtmlResponse extends Response implements CacheableResponseInterface, AttachmentsResponseInterface { +class HtmlResponse extends Response implements CacheableResponseInterface, AttachmentsInterface { use CacheableResponseTrait; use AttachmentsResponseTrait; diff --git a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php index d143d83..50ff584 100644 --- a/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php +++ b/core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php @@ -93,7 +93,7 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor /** * {@inheritdoc} */ - public function processAttachments(AttachmentsResponseInterface $response) { + public function processAttachments(AttachmentsInterface $response) { // @todo Convert to assertion once https://www.drupal.org/node/2408013 lands if (!$response instanceof HtmlResponse) { throw new \InvalidArgumentException('\Drupal\Core\Render\HtmlResponse instance expected.');