function DynamicPageCacheSubscriber::responseToRenderArray

Same name and namespace in other branches
  1. 9 core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::responseToRenderArray()

Embeds a Response object in a render array so that RenderCache can cache it.

@todo Refactor/remove once https://www.drupal.org/node/2551419 lands.

Parameters

\Drupal\Core\Cache\CacheableResponseInterface $response: A cacheable response.

Return value

array A render array that embeds the given cacheable response object, with the cacheability metadata of the response object present in the #cache property of the render array.

See also

renderArrayToResponse()

1 call to DynamicPageCacheSubscriber::responseToRenderArray()
DynamicPageCacheSubscriber::onResponse in core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php
Stores a response in case of a Dynamic Page Cache miss, if cacheable.

File

core/modules/dynamic_page_cache/src/EventSubscriber/DynamicPageCacheSubscriber.php, line 267

Class

DynamicPageCacheSubscriber
Returns cached responses as early and avoiding as much work as possible.

Namespace

Drupal\dynamic_page_cache\EventSubscriber

Code

protected function responseToRenderArray(CacheableResponseInterface $response) {
    $response_as_render_array = $this->dynamicPageCacheRedirectRenderArray + [
        // The data we actually care about.
'#response' => $response,
        // Tell RenderCache to cache the #response property: the data we actually
        // care about.
'#cache_properties' => [
            '#response',
        ],
        // These exist only to fulfill the requirements of the RenderCache, which
        // is designed to work with render arrays only. We don't care about these.
'#markup' => '',
        '#attached' => '',
    ];
    // Merge the response's cacheability metadata, so that RenderCache can take
    // care of cache redirects for us.
    CacheableMetadata::createFromObject($response->getCacheableMetadata())
        ->merge(CacheableMetadata::createFromRenderArray($response_as_render_array))
        ->applyTo($response_as_render_array);
    return $response_as_render_array;
}

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