You are here

protected function JsonApiDocumentTopLevelNormalizer::normalizeOmissionsLinks in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer::normalizeOmissionsLinks()

Normalizes omitted data into a set of omission links.

@todo: refactor this to use link collections in https://www.drupal.org/project/drupal/issues/3036279.

Parameters

\Drupal\jsonapi\JsonApiResource\OmittedData $omissions: The omitted response data.

string $format: The normalization format.

array $context: The normalization context.

Return value

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization|\Drupal\jsonapi\Normalizer\Value\CacheableOmission The normalized omissions.

1 call to JsonApiDocumentTopLevelNormalizer::normalizeOmissionsLinks()
JsonApiDocumentTopLevelNormalizer::normalize in core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php

File

core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php, line 252

Class

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

Namespace

Drupal\jsonapi\Normalizer

Code

protected function normalizeOmissionsLinks(OmittedData $omissions, $format, array $context = []) {
  $normalized_omissions = array_map(function (HttpExceptionInterface $exception) use ($format, $context) {
    return $this->serializer
      ->normalize($exception, $format, $context);
  }, $omissions
    ->toArray());
  $cacheability = CacheableMetadata::createFromObject(CacheableNormalization::aggregate($normalized_omissions));
  if (empty($normalized_omissions)) {
    return new CacheableOmission($cacheability);
  }
  $omission_links = [
    'detail' => 'Some resources have been omitted because of insufficient authorization.',
    'links' => [
      'help' => [
        'href' => 'https://www.drupal.org/docs/8/modules/json-api/filtering#filters-access-control',
      ],
    ],
  ];
  $link_hash_salt = Crypt::randomBytesBase64();
  foreach ($normalized_omissions as $omission) {
    $cacheability
      ->addCacheableDependency($omission);

    // Add the errors to the pre-existing errors.
    foreach ($omission
      ->getNormalization() as $error) {

      // JSON:API links cannot be arrays and the spec generally favors link
      // relation types as keys. 'item' is the right link relation type, but
      // we need multiple values. To do that, we generate a meaningless,
      // random value to use as a unique key. That value is a hash of a
      // random salt and the link href. This ensures that the key is non-
      // deterministic while letting use deduplicate the links by their
      // href. The salt is *not* used for any cryptographic reason.
      $link_key = 'item--' . static::getLinkHash($link_hash_salt, $error['links']['via']['href']);
      $omission_links['links'][$link_key] = [
        'href' => $error['links']['via']['href'],
        'meta' => [
          'rel' => 'item',
          'detail' => $error['detail'],
        ],
      ];
    }
  }
  return new CacheableNormalization($cacheability, $omission_links);
}