You are here

protected function JsonApiDocumentTopLevelNormalizer::normalizeEntityReferenceFieldItemList in JSON:API 8.2

Normalizes an entity reference field, i.e. a relationship document.

@todo: remove this in https://www.drupal.org/project/jsonapi/issues/3036285.

Parameters

\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel $document: The document to normalize.

string $format: The normalization format.

array $context: The normalization context.

Return value

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization The normalized document.

1 call to JsonApiDocumentTopLevelNormalizer::normalizeEntityReferenceFieldItemList()
JsonApiDocumentTopLevelNormalizer::normalize in src/Normalizer/JsonApiDocumentTopLevelNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/JsonApiDocumentTopLevelNormalizer.php, line 258

Class

JsonApiDocumentTopLevelNormalizer
Normalizes the top-level document according to the JSON:API specification.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function normalizeEntityReferenceFieldItemList(JsonApiDocumentTopLevel $document, $format, array $context = []) {
  $data = $document
    ->getData();
  $parent_entity = $data
    ->getEntity();
  $resource_type = $this->resourceTypeRepository
    ->get($parent_entity
    ->getEntityTypeId(), $parent_entity
    ->bundle());
  $context['resource_object'] = ResourceObject::createFromEntity($resource_type, $parent_entity);
  $normalized_relationship = $this->serializer
    ->normalize($data, $format, $context);
  assert($normalized_relationship instanceof CacheableNormalization);
  unset($context['resource_object']);
  return new CacheableNormalization($normalized_relationship, $normalized_relationship
    ->getNormalization()['data']);
}