diff --git a/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php b/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php deleted file mode 100644 index 8c91eba..0000000 --- a/core/lib/Drupal/Core/Cache/ConditionalCacheabilityMetadataBubblingTrait.php +++ /dev/null @@ -1,32 +0,0 @@ -renderer->hasRenderContext()) { - $build = []; - CacheableMetadata::createFromObject($object)->applyTo($build); - $this->renderer->render($build); - } - } - -} diff --git a/core/modules/image/src/Normalizer/ImageItemHalNormalizer.php b/core/modules/image/src/Normalizer/ImageItemHalNormalizer.php index a4001f8..c65b399 100644 --- a/core/modules/image/src/Normalizer/ImageItemHalNormalizer.php +++ b/core/modules/image/src/Normalizer/ImageItemHalNormalizer.php @@ -2,7 +2,6 @@ namespace Drupal\image\Normalizer; -use Drupal\Core\Cache\ConditionalCacheabilityMetadataBubblingTrait; use Drupal\Core\Render\RendererInterface; use Drupal\hal\Normalizer\EntityReferenceItemNormalizer; use Drupal\image\Plugin\Field\FieldType\ImageItem; @@ -14,8 +13,6 @@ */ class ImageItemHalNormalizer extends EntityReferenceItemNormalizer { - use ConditionalCacheabilityMetadataBubblingTrait; - use ImageItemNormalizerTrait; /** @@ -53,7 +50,7 @@ public function normalize($field_item, $format = NULL, array $context = []) { $normalization = parent::normalize($field_item, $format, $context); if (!$field_item->isEmpty()) { $field_key = array_keys($normalization['_embedded'])[0]; - $this->decorateWithImageStyles($field_item, $normalization['_embedded'][$field_key][0]); + $this->decorateWithImageStyles($field_item, $normalization['_embedded'][$field_key][0], $context); } return $normalization; } diff --git a/core/modules/image/src/Normalizer/ImageItemNormalizer.php b/core/modules/image/src/Normalizer/ImageItemNormalizer.php index f8c20da..93fc3df 100644 --- a/core/modules/image/src/Normalizer/ImageItemNormalizer.php +++ b/core/modules/image/src/Normalizer/ImageItemNormalizer.php @@ -2,7 +2,6 @@ namespace Drupal\image\Normalizer; -use Drupal\Core\Cache\ConditionalCacheabilityMetadataBubblingTrait; use Drupal\Core\Render\RendererInterface; use Drupal\image\Plugin\Field\FieldType\ImageItem; use Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer; @@ -12,8 +11,6 @@ */ class ImageItemNormalizer extends EntityReferenceFieldItemNormalizer { - use ConditionalCacheabilityMetadataBubblingTrait; - use ImageItemNormalizerTrait; /** @@ -45,7 +42,7 @@ public function normalize($field_item, $format = NULL, array $context = []) { /* @var \Drupal\image\Plugin\Field\FieldType\ImageItem $field_item */ $normalization = parent::normalize($field_item, $format, $context); if (!$field_item->isEmpty()) { - $this->decorateWithImageStyles($field_item, $normalization); + $this->decorateWithImageStyles($field_item, $normalization, $context); } return $normalization; diff --git a/core/modules/image/src/Normalizer/ImageItemNormalizerTrait.php b/core/modules/image/src/Normalizer/ImageItemNormalizerTrait.php index e28e56b..d5e850d 100644 --- a/core/modules/image/src/Normalizer/ImageItemNormalizerTrait.php +++ b/core/modules/image/src/Normalizer/ImageItemNormalizerTrait.php @@ -2,6 +2,7 @@ namespace Drupal\image\Normalizer; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\file\Entity\File; use Drupal\image\Entity\ImageStyle; use Drupal\image\Plugin\Field\FieldType\ImageItem; @@ -18,8 +19,10 @@ * The image field item. * @param array $normalization * The image field normalization to add image style information to. + * @param array $context + * Context options for the normalizer. */ - protected function decorateWithImageStyles(ImageItem $item, array &$normalization) { + protected function decorateWithImageStyles(ImageItem $item, array &$normalization, array $context) { /** @var \Drupal\file\FileInterface $image */ if ($image = File::load($item->target_id)) { $uri = $image->getFileUri(); @@ -34,7 +37,9 @@ protected function decorateWithImageStyles(ImageItem $item, array &$normalizatio 'height' => empty($dimensions['height']) ? NULL : $dimensions['height'], 'width' => empty($dimensions['width']) ? NULL : $dimensions['width'], ]; - $this->bubble($style); + if (!empty($context['cacheable_metadata'])) { + $context['cacheable_metadata']->addCacheableDependency(CacheableMetadata::createFromObject($style)); + } } } } diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php index ca0eaa2..7e4cad8 100644 --- a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php +++ b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemHalNormalizerTest.php @@ -25,12 +25,4 @@ protected function getNormalizedImageStyles(array $normalization) { return array_pop($normalization['_embedded'])[0]['image_styles']; } - /** - * {@inheritdoc} - */ - protected function getExpectedCacheability() { - $cacheability = parent::getExpectedCacheability(); - return $cacheability->setCacheContexts(['url.site']); - } - } diff --git a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php index 918b52b..4870af7 100644 --- a/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php +++ b/core/modules/image/tests/src/Kernel/Normalizer/ImageItemNormalizerTestBase.php @@ -3,9 +3,8 @@ namespace Drupal\Tests\image\Kernel\Normalizer; use Drupal\Core\Cache\Cache; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Field\FieldStorageDefinitionInterface; -use Drupal\Core\Render\BubbleableMetadata; -use Drupal\Core\Render\RenderContext; use Drupal\entity_test\Entity\EntityTest; use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldStorageConfig; @@ -109,14 +108,11 @@ public function testNormalize() { $original_entity->save(); $entity = clone $original_entity; - $context = new RenderContext(); - $normalization = $this->container->get('renderer') - ->executeInRenderContext($context, function () use ($entity) { - return $this->serializer->normalize($entity, $this->format); - }); + $cacheability_metadata = new CacheableMetadata(); + $normalization = $this->serializer->normalize($entity, $this->format, ['cacheable_metadata' => $cacheability_metadata]); $normalized_image_styles = $this->getNormalizedImageStyles($normalization); - $this->assertEquals($this->getExpectedCacheability(), $context->pop()); + $this->assertEquals($this->getExpectedCacheability(), $cacheability_metadata); $expect_dimensions = [ 'large' => [ 'width' => '422', @@ -154,11 +150,11 @@ public function testNormalize() { /** * Gets the expected bubbled cacheability metadata. * - * @return \Drupal\Core\Render\BubbleableMetadata - * The expected bubbled cacheability metadata. + * @return \Drupal\Core\Cache\CacheableMetadata + * The expected cacheability metadata. */ protected function getExpectedCacheability() { - $cacheability = new BubbleableMetadata(); + $cacheability = new CacheableMetadata(); $cache_tags = []; /** @var \Drupal\image\ImageStyleInterface $image_style */ foreach (ImageStyle::loadMultiple() as $image_style) { diff --git a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php index 5cb887b..59c1954 100644 --- a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php +++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php @@ -151,14 +151,14 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac // If there is data to send, serialize and set it as the response body. if ($data !== NULL) { $context = new RenderContext(); - $output = $this->renderer - ->executeInRenderContext($context, function () use ($serializer, $data, $format) { - return $serializer->serialize($data, $format); - }); - - if ($response instanceof CacheableResponseInterface && !$context->isEmpty()) { - $response->addCacheableDependency($context->pop()); + $serialization_contexts = ['request' => $request]; + if ($response instanceof CacheableResponseInterface) { + if (!$context->isEmpty()) { + $response->addCacheableDependency($context->pop()); + } + $serialization_contexts['cacheable_metadata'] = $response->getCacheableMetadata(); } + $output = $serializer->serialize($data, $format, $serialization_contexts); $response->setContent($output); $response->headers->set('Content-Type', $request->getMimeType($format));