Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
updated file
Browse files Browse the repository at this point in the history
  • Loading branch information
yeniatencio committed May 9, 2024
1 parent 813aadf commit 8587bda
Showing 1 changed file with 29 additions and 34 deletions.
63 changes: 29 additions & 34 deletions src/EventSubscriber/TideSiteResourceObjectNormalizationCacher.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\tide_site\EventSubscriber;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\VariationCacheInterface;
use Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher;
use Drupal\jsonapi\JsonApiResource\ResourceObject;

Expand All @@ -12,59 +13,53 @@
class TideSiteResourceObjectNormalizationCacher extends ResourceObjectNormalizationCacher {

/**
* Writes a normalization to cache.
*
* @see \Drupal\jsonapi\EventSubscriber\ResourceObjectNormalizationCacher
* Writes a normalization to cache.
* @param \Drupal\jsonapi\JsonApiResource\ResourceObject $object
* The resource object for which to generate a cache item.
* @param array $normalization_parts
* The normalization parts to cache.
*/
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' => '',
];
// @todo Investigate whether to cache POST and PATCH requests.
// @todo Follow up on https://www.drupal.org/project/drupal/issues/3381898.
if (!$this->requestStack
->getCurrentRequest()
->isMethodCacheable()) {
return;
}

// 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 the entity's cacheability metadata with that of the normalization
// parts, so that VariationCache can take care of cache redirects for us.
$cacheability = CacheableMetadata::createFromObject($object)
// Adds url.query_args:site to the normalization.
->addCacheContexts(['url.query_args:site'])
->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);
->merge(static::mergeCacheableDependencies($normalization_parts[static::RESOURCE_CACHE_SUBSET_FIELDS]));

$this->renderCache->set($data_as_render_array, $base);
$this->variationCache
->set($this->generateCacheKeys($object), $normalization_parts, $cacheability, new CacheableMetadata());
}

/**
* Generates a lookup render array for a normalization.
* Generates the cache keys for a normalization.
*
* @param \Drupal\jsonapi\JsonApiResource\ResourceObject $object
* The resource object for which to generate a cache item.
* The resource object for which to generate the cache keys.
*
* @return array
* A render array for use with the RenderCache service.
* @return string[]
* The cache keys to pass to the variation cache.
*
* @see \Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber::$dynamicPageCacheRedirectRenderArray
*/
protected static function generateLookupRenderArray(ResourceObject $object) {
return [
'#cache' => [
'keys' => [
protected static function generateCacheKeys(ResourceObject $object) {
return [
$object->getResourceType()
->getTypeName(),
->getTypeName(),
$object->getId(),
],
'bin' => 'jsonapi_normalizations',
],
];
$object->getLanguage()
->getId(),
];
}

}

0 comments on commit 8587bda

Please sign in to comment.