diff --git a/core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php b/core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php index ca88533a7a2931f55a863ae97b3973cde8647feb..1e801281487cf9f50959cab5b14b00f6e0b76629 100644 --- a/core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php +++ b/core/modules/jsonapi/src/EventSubscriber/ResourceObjectNormalizationCacher.php @@ -164,7 +164,7 @@ protected function set(ResourceObject $object, array $normalization_parts) { protected static function generateLookupRenderArray(ResourceObject $object) { return [ '#cache' => [ - 'keys' => [$object->getResourceType()->getTypeName(), $object->getId()], + 'keys' => [$object->getResourceType()->getTypeName(), $object->getId(), $object->getLanguage()->getId()], 'bin' => 'jsonapi_normalizations', ], ]; diff --git a/core/modules/jsonapi/src/JsonApiResource/ResourceObject.php b/core/modules/jsonapi/src/JsonApiResource/ResourceObject.php index 973bde313688e86fac3b7751e8c54fe03a1b6bc8..add89868f7221482e5c1f2d7732e9507d3a325e8 100644 --- a/core/modules/jsonapi/src/JsonApiResource/ResourceObject.php +++ b/core/modules/jsonapi/src/JsonApiResource/ResourceObject.php @@ -9,6 +9,8 @@ use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\RevisionableInterface; +use Drupal\Core\Language\Language; +use Drupal\Core\Language\LanguageInterface; use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper; use Drupal\Core\Url; use Drupal\jsonapi\JsonApiSpec; @@ -60,6 +62,13 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier */ protected $links; + /** + * The resource language. + * + * @var \Drupal\Core\Language\LanguageInterface + */ + protected $language; + /** * ResourceObject constructor. * @@ -76,8 +85,10 @@ class ResourceObject implements CacheableDependencyInterface, ResourceIdentifier * An array of the resource object's fields, keyed by public field name. * @param \Drupal\jsonapi\JsonApiResource\LinkCollection $links * The links for the resource object. + * @param \Drupal\Core\Language\LanguageInterface|null $language + * (optional) The resource language. */ - public function __construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type, $id, $revision_id, array $fields, LinkCollection $links) { + public function __construct(CacheableDependencyInterface $cacheability, ResourceType $resource_type, $id, $revision_id, array $fields, LinkCollection $links, LanguageInterface $language = NULL) { assert(is_null($revision_id) || $resource_type->isVersionable()); $this->setCacheability($cacheability); $this->resourceType = $resource_type; @@ -85,6 +96,10 @@ public function __construct(CacheableDependencyInterface $cacheability, Resource $this->versionIdentifier = $revision_id ? 'id:' . $revision_id : NULL; $this->fields = $fields; $this->links = $links->withContext($this); + + // If the specified language empty it falls back the same way as in the entity system + // @see \Drupal\Core\Entity\EntityBase::language() + $this->language = $language ?: new Language(['id' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); } /** @@ -109,7 +124,8 @@ public static function createFromEntity(ResourceType $resource_type, EntityInter $entity->uuid(), $resource_type->isVersionable() && $entity instanceof RevisionableInterface ? $entity->getRevisionId() : NULL, static::extractFieldsFromEntity($resource_type, $entity), - static::buildLinksFromEntity($resource_type, $entity, $links ?: new LinkCollection([])) + static::buildLinksFromEntity($resource_type, $entity, $links ?: new LinkCollection([])), + $entity->language() ); } @@ -153,6 +169,16 @@ public function getFields() { return $this->fields; } + /** + * Gets the ResourceObject's language. + * + * @return \Drupal\Core\Language\LanguageInterface + * The resource language. + */ + public function getLanguage(): LanguageInterface { + return $this->language; + } + /** * Gets the ResourceObject's links. * diff --git a/core/modules/jsonapi/tests/src/Functional/NodeTest.php b/core/modules/jsonapi/tests/src/Functional/NodeTest.php index 411e0738d6fdc3f54c1fc039d13316c04a466aa2..6e3d99a76236453976edf4dd7316f1d986527a8f 100644 --- a/core/modules/jsonapi/tests/src/Functional/NodeTest.php +++ b/core/modules/jsonapi/tests/src/Functional/NodeTest.php @@ -377,9 +377,10 @@ protected function assertCacheableNormalizations(): void { // Save the entity to invalidate caches. $this->entity->save(); $uuid = $this->entity->uuid(); + $language = $this->entity->language()->getId(); $cache = \Drupal::service('render_cache')->get([ '#cache' => [ - 'keys' => ['node--camelids', $uuid], + 'keys' => ['node--camelids', $uuid, $language], 'bin' => 'jsonapi_normalizations', ], ]); @@ -415,7 +416,7 @@ protected function assertCacheableNormalizations(): void { protected function assertNormalizedFieldsAreCached(array $field_names): void { $cache = \Drupal::service('render_cache')->get([ '#cache' => [ - 'keys' => ['node--camelids', $this->entity->uuid()], + 'keys' => ['node--camelids', $this->entity->uuid(), $this->entity->language()->getId()], 'bin' => 'jsonapi_normalizations', ], ]);