function LinkCollectionNormalizer::normalize

Same name in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/LinkCollectionNormalizer.php \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer::normalize()
  2. 10 core/modules/jsonapi/src/Normalizer/LinkCollectionNormalizer.php \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer::normalize()
  3. 11.x core/modules/jsonapi/src/Normalizer/LinkCollectionNormalizer.php \Drupal\jsonapi\Normalizer\LinkCollectionNormalizer::normalize()

File

core/modules/jsonapi/src/Normalizer/LinkCollectionNormalizer.php, line 68

Class

LinkCollectionNormalizer
Normalizes a LinkCollection object.

Namespace

Drupal\jsonapi\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {
    assert($object instanceof LinkCollection);
    $normalized = [];
    
    /* @var \Drupal\jsonapi\JsonApiResource\Link $link */
    foreach ($object as $key => $links) {
        $is_multiple = count($links) > 1;
        foreach ($links as $link) {
            $link_key = $is_multiple ? sprintf('%s--%s', $key, $this->hashByHref($link)) : $key;
            $attributes = $link->getTargetAttributes();
            $normalization = array_merge([
                'href' => $link->getHref(),
            ], !empty($attributes) ? [
                'meta' => $attributes,
            ] : []);
            $normalized[$link_key] = new CacheableNormalization($link, $normalization);
        }
    }
    return CacheableNormalization::aggregate($normalized);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.