Unverified Commit 79745314 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3199696 by bbrala, Wim Leers, plach, bradjones1, gabesullice: Add...

Issue #3199696 by bbrala, Wim Leers, plach, bradjones1, gabesullice: Add language support to ResourceObject

(cherry picked from commit f87303bf)
parent 62dc0bc4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -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',
      ],
    ];
+28 −2
Original line number Diff line number Diff line
@@ -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.
   *
+3 −2
Original line number Diff line number Diff line
@@ -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',
      ],
    ]);