class HtmlResponse

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

A response that contains and can expose cacheability metadata and attachments.

Supports Drupal's caching concepts: cache tags for invalidation and cache contexts for variations.

Supports Drupal's idea of #attached metadata: libraries, settings, http_header and html_head.

Hierarchy

Expanded class hierarchy of HtmlResponse

See also

\Drupal\Core\Cache\CacheableResponse

\Drupal\Core\Render\AttachmentsInterface

\Drupal\Core\Render\AttachmentsTrait

12 files declare their use of HtmlResponse
BigPipe.php in core/modules/big_pipe/src/Render/BigPipe.php
BigPipeResponse.php in core/modules/big_pipe/src/Render/BigPipeResponse.php
BigPipeResponseAttachmentsProcessor.php in core/modules/big_pipe/src/Render/BigPipeResponseAttachmentsProcessor.php
BigPipeResponseAttachmentsProcessorTest.php in core/modules/big_pipe/tests/src/Unit/Render/BigPipeResponseAttachmentsProcessorTest.php
BigPipeTestSubscriber.php in core/modules/big_pipe/tests/modules/big_pipe_test/src/EventSubscriber/BigPipeTestSubscriber.php

... See full list

File

core/lib/Drupal/Core/Render/HtmlResponse.php, line 22

Namespace

Drupal\Core\Render
View source
class HtmlResponse extends Response implements CacheableResponseInterface, AttachmentsInterface {
    use CacheableResponseTrait;
    use AttachmentsTrait;
    
    /**
     * {@inheritdoc}
     */
    public function setContent($content) {
        // A render array can automatically be converted to a string and set the
        // necessary metadata.
        if (is_array($content) && isset($content['#markup'])) {
            $content += [
                '#attached' => [
                    'html_response_attachment_placeholders' => [],
                    'placeholders' => [],
                ],
            ];
            $this->addCacheableDependency(CacheableMetadata::createFromRenderArray($content));
            $this->setAttachments($content['#attached']);
            $content = $content['#markup'];
        }
        return parent::setContent($content);
    }

}

Members

Title Sort descending Modifiers Object type Summary
AttachmentsTrait::$attachments protected property The attachments for this response.
AttachmentsTrait::addAttachments public function
AttachmentsTrait::getAttachments public function
AttachmentsTrait::setAttachments public function
CacheableResponseTrait::$cacheabilityMetadata protected property The cacheability metadata.
CacheableResponseTrait::addCacheableDependency public function
CacheableResponseTrait::getCacheableMetadata public function
HtmlResponse::setContent public function

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