3 namespace Drupal\Core\Render;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\CacheableResponseInterface;
7 use Drupal\Core\Cache\CacheableResponseTrait;
8 use Symfony\Component\HttpFoundation\Response;
11 * A response that contains and can expose cacheability metadata and attachments.
13 * Supports Drupal's caching concepts: cache tags for invalidation and cache
14 * contexts for variations.
16 * Supports Drupal's idea of #attached metadata: libraries, settings, http_header and html_head.
18 * @see \Drupal\Core\Cache\CacheableResponse
19 * @see \Drupal\Core\Render\AttachmentsInterface
20 * @see \Drupal\Core\Render\AttachmentsTrait
22 class HtmlResponse extends Response implements CacheableResponseInterface, AttachmentsInterface {
24 use CacheableResponseTrait;
30 public function setContent($content) {
31 // A render array can automatically be converted to a string and set the
32 // necessary metadata.
33 if (is_array($content) && (isset($content['#markup']))) {
34 $content += ['#attached' => [
35 'html_response_attachment_placeholders' => [],
36 'placeholders' => []],
38 $this->addCacheableDependency(CacheableMetadata::createFromRenderArray($content));
39 $this->setAttachments($content['#attached']);
40 $content = $content['#markup'];
43 return parent::setContent($content);