You are here

protected function ResourceObjectNormalizationCacher::set in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher::set()

Writes a normalization to cache.

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

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObject $object: The resource object for which to generate a cache item.

array $normalization_parts: The normalization parts to cache.

See also

\Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::responseToRenderArray()

1 call to ResourceObjectNormalizationCacher::set()
ResourceObjectNormalizationCacher::onTerminate in core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php
Writes normalizations of entities to cache, if any were created.

File

core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php, line 129

Class

ResourceObjectNormalizationCacher
Caches entity normalizations after the response has been sent.

Namespace

Drupal\jsonapi\EventSubscriber

Code

protected function set(ResourceObject $object, array $normalization_parts) {
  $base = static::generateLookupRenderArray($object);
  $data_as_render_array = $base + [
    // The data we actually care about.
    '#data' => $normalization_parts,
    // Tell RenderCache to cache the #data property: the data we actually care
    // about.
    '#cache_properties' => [
      '#data',
    ],
    // 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 entity's cacheability metadata with that of the normalization
  // parts, so that RenderCache can take care of cache redirects for us.
  CacheableMetadata::createFromObject($object)
    ->merge(static::mergeCacheableDependencies($normalization_parts[static::RESOURCE_CACHE_SUBSET_BASE]))
    ->merge(static::mergeCacheableDependencies($normalization_parts[static::RESOURCE_CACHE_SUBSET_FIELDS]))
    ->applyTo($data_as_render_array);
  $this->renderCache
    ->set($data_as_render_array, $base);
}