DynamicPageCacheSubscriber::responseToRenderArray

protected DynamicPageCacheSubscriber::responseToRenderArray(CacheableResponseInterface $response)

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()

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;
}
doc_Drupal
2016-10-29 09:03:28
Comments
Leave a Comment

Please login to continue.