You are here

protected static function LinkCollectionNormalizer::addLinkRels in Consumer Image Styles 8.3

Same name and namespace in other branches
  1. 4.x src/Normalizer/LinkCollectionNormalizer.php \Drupal\consumer_image_styles\Normalizer\LinkCollectionNormalizer::addLinkRels()

Adds the derivative link relation type to the normalized link collection.

Parameters

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization $cacheable_normalization: The cacheable normalization to which link relations need to be added.

\Drupal\jsonapi\JsonApiResource\LinkCollection $link_collection: The un-normalized link collection.

Return value

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization The links normalization with meta.rel added.

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

File

src/Normalizer/LinkCollectionNormalizer.php, line 160

Class

LinkCollectionNormalizer
Custom normalizer that add the derivatives to image entities.

Namespace

Drupal\consumer_image_styles\Normalizer

Code

protected static function addLinkRels(CacheableNormalization $cacheable_normalization, LinkCollection $link_collection) {
  $normalization = $cacheable_normalization
    ->getNormalization();
  foreach ($normalization as $key => &$normalized_link) {
    $links = iterator_to_array($link_collection);
    if (isset($links[$key])) {
      $normalized_link['meta']['rel'] = array_reduce($links[$key], function (array $relations, Link $link) {
        return array_unique(array_merge($relations, $link
          ->getLinkRelationTypes()));
      }, []);
    }
  }
  return new CacheableNormalization($cacheable_normalization, $normalization);
}