You are here

class HtmlResponse in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/HtmlResponse.php \Drupal\Core\Render\HtmlResponse

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 23

Namespace

Drupal\Core\Render
View source
class HtmlResponse extends Response implements CacheableResponseInterface, AttachmentsInterface {
  use CacheableResponseTrait;
  use AttachmentsTrait;

  /**
   * Constructs a HtmlResponse object.
   *
   * @param array|string $content
   *   The response content, see setContent().
   * @param int $status
   *   The response status code.
   * @param array $headers
   *   An array of response headers.
   *
   * @throws \InvalidArgumentException
   *   When the HTTP status code is not valid.
   */
  public function __construct($content = '', int $status = 200, array $headers = []) {
    parent::__construct('', $status, $headers);
    $this
      ->setContent($content);
  }

  /**
   * Sets the response content.
   *
   * @param mixed $content
   *   Content that can be cast to string, or a render array.
   *
   * @return $this
   */
  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

Namesort descending Modifiers Type Description Overrides
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 Sets the response content.
HtmlResponse::__construct public function Constructs a HtmlResponse object. 1