if ($data instanceof ErrorCollection
) { $document['errors'
] =
$this->
normalizeErrorDocument($object,
$format,
$context);
} else { // Add data.
$document['data'
] =
$this->serializer->
normalize($data,
$format,
$context);
// Add includes.
$document['included'
] =
$this->serializer->
normalize($object->
getIncludes(),
$format,
$context)->
omitIfEmpty();
// Add omissions and metadata.
$normalized_omissions =
$this->
normalizeOmissionsLinks($object->
getOmissions(),
$format,
$context);
$meta = !
$normalized_omissions instanceof CacheableOmission
?
array_merge($object->
getMeta(),
['omitted' =>
$normalized_omissions->
getNormalization()]) :
$object->
getMeta();
$document['meta'
] =
(new CacheableNormalization($normalized_omissions,
$meta))->
omitIfEmpty();
} // Add document links.
$document['links'
] =
$this->serializer->
normalize($object->
getLinks(),
$format,
$context)->
omitIfEmpty();
// Every JSON:API document contains absolute URLs.
return CacheableNormalization::
aggregate($document)->
withCacheableDependency((new CacheableMetadata())->
addCacheContexts(['url.site'
]));
} /**
* Normalizes an error collection.
*
* @param \Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel $document
* The document to normalize.
* @param string $format
* The normalization format.
* @param array $context
* The normalization context.
*
* @return \Drupal\jsonapi\Normalizer\Value\CacheableNormalization
* The normalized document.
*
* @todo: refactor this to use CacheableNormalization::aggregate in https://www.drupal.org/project/drupal/issues/3036284.
*/