Skip to content
Snippets Groups Projects
Commit 713fc5da authored by Brad Jones's avatar Brad Jones Committed by Björn Brala
Browse files

Issue #3071310 by bradjones1, ndobromirov, bbrala, e0ipso: Field enhancers...

Issue #3071310 by bradjones1, ndobromirov, bbrala, e0ipso: Field enhancers need to support cacheability metadata bubbling
parent b4692a25
No related branches found
No related tags found
1 merge request!5Issue #3071310: Field enhancers need to support cacheability metadata bubbling
......@@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\jsonapi\Normalizer\FieldItemNormalizer as JsonapiFieldItemNormalizer;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi_extras\Plugin\ResourceFieldEnhancerManager;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Shaper\Util\Context;
/**
......@@ -57,12 +58,22 @@ class FieldItemNormalizer extends JsonApiNormalizerDecoratorBase {
if (!$enhancer) {
return $normalized_output;
}
// Apply any enhancements necessary.
$context['field_item_object'] = $object;
$processed = $enhancer->undoTransform($normalized_output->getNormalization(), new Context($context));
$cacheability = CacheableMetadata::createFromObject($normalized_output)
->addCacheTags(['config:jsonapi_resource_config_list']);
$normalized_output = new CacheableNormalization($cacheability, $processed);
// Apply any enhancements necessary.
$context = new Context([
'field_item_object' => $object,
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => $cacheability,
]);
$processed = $enhancer->undoTransform(
$normalized_output->getNormalization(),
$context
);
$normalized_output = new CacheableNormalization(
// This was passed by reference but often, merging creates a new object.
$context->offsetGet(CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY),
$processed
);
return $normalized_output;
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\jsonapi_extras\Normalizer;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\jsonapi\JsonApiResource\ResourceIdentifier;
......@@ -9,6 +10,7 @@ use Drupal\jsonapi\JsonApiResource\ResourceObject;
use Drupal\jsonapi\Normalizer\Value\CacheableNormalization;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
use Drupal\jsonapi_extras\ResourceType\ConfigurableResourceType;
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
use Shaper\Util\Context;
/**
......@@ -60,12 +62,21 @@ class ResourceIdentifierNormalizer extends JsonApiNormalizerDecoratorBase {
if (!$enhancer) {
return $normalized_output;
}
$cacheability = CacheableMetadata::createFromObject($normalized_output)
->addCacheTags(['config:jsonapi_resource_config_list']);
// Apply any enhancements necessary.
$context['field_resource_identifier'] = $field;
$transformed = $enhancer->undoTransform($normalized_output->getNormalization(), new Context($context));
// @todo Enhancers should utilize CacheableNormalization to infer additional cacheability from the enhancer.
$context = new Context([
'field_resource_identifier' => $field,
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => $cacheability,
]);
$transformed = $enhancer->undoTransform(
$normalized_output->getNormalization(),
$context
);
return new CacheableNormalization(
$normalized_output,
// This was passed by reference but often, merging creates a new object.
$context->offsetGet(CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY),
array_intersect_key($transformed, array_flip(['id', 'type', 'meta']))
);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment