.../EventSubscriber/ResourceResponseSubscriber.php | 30 ++++++++++++++-------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php index 99d99de..9b12412 100644 --- a/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php +++ b/core/modules/rest/src/EventSubscriber/ResourceResponseSubscriber.php @@ -22,6 +22,15 @@ class ResourceResponseSubscriber implements EventSubscriberInterface { /** + * Name of key for bubbling cacheability metadata via serialization context. + * + * @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() + * @see \Symfony\Component\Serializer\SerializerInterface::serialize() + * @see \Drupal\rest\EventSubscriber\ResourceResponseSubscriber::renderResponseBody() + */ + const SERIALIZATION_CONTEXT_CACHEABILITY = 'cacheability'; + + /** * The serializer. * * @var \Symfony\Component\Serializer\SerializerInterface @@ -134,13 +143,14 @@ public function getResponseFormat(RouteMatchInterface $route_match, Request $req * received context. This bubbled cacheability metadata will be applied to the * the response. * - * In prior versions of Drupal 8, we allowed implicit bubbling of cacheability - * metadata because there was no explicit cacheability metadata bubbling API. - * To maintain backwards compatibility, we continue to support this, but - * support for this will be dropped in Drupal 9.0.0. This is especially useful - * when interacting with APIs that implicitly invoke rendering (for example: - * generating URLs): this allows those to "leak", and we collect their bubbled - * cacheability metadata automatically in a render context. + * In versions of Drupal prior to 8.5, implicit bubbling of cacheability + * metadata was allowed because there was no explicit cacheability metadata + * bubbling API. To maintain backwards compatibility, we continue to support + * this, but support for this will be dropped in Drupal 9.0.0. This is + * especially useful when interacting with APIs that implicitly invoke + * rendering (for example: generating URLs): this allows those to "leak", and + * we collect their bubbled cacheability metadata automatically in a render + * context. * * @param \Symfony\Component\HttpFoundation\Request $request * The request object. @@ -162,7 +172,7 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac if ($data !== NULL) { $serialization_context = [ 'request' => $request, - 'cacheability' => new CacheableMetadata(), + static::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(), ]; // @deprecated In Drupal 8.5.0, will be removed before Drupal 9.0.0. Use @@ -175,10 +185,10 @@ protected function renderResponseBody(Request $request, ResourceResponseInterfac }); if ($response instanceof CacheableResponseInterface) { if (!$context->isEmpty()) { - @trigger_error('Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.4.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling.', E_USER_DEPRECATED); + @trigger_error('Implicit cacheability metadata bubbling (onto the global render context) in normalizers is deprecated since Drupal 8.5.0 and will be removed in Drupal 9.0.0. Use the "cacheability" serialization context instead, for explicit cacheability metadata bubbling. See https://www.drupal.org/node/2918937', E_USER_DEPRECATED); $response->addCacheableDependency($context->pop()); } - $response->addCacheableDependency($serialization_context['cacheability']); + $response->addCacheableDependency($serialization_context[static::SERIALIZATION_CONTEXT_CACHEABILITY]); } $response->setContent($output);