You are here

protected function LinkCollectionNormalizer::buildVariantLinks 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::buildVariantLinks()

Parameters

\Drupal\jsonapi\JsonApiResource\ResourceObject $resource_object:

\Drupal\consumers\Entity\Consumer:

Return value

\Drupal\jsonapi\JsonApiResource\LinkCollection The variant links.

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

File

src/Normalizer/LinkCollectionNormalizer.php, line 101

Class

LinkCollectionNormalizer
Custom normalizer that add the derivatives to image entities.

Namespace

Drupal\consumer_image_styles\Normalizer

Code

protected function buildVariantLinks(ResourceObject $resource_object, Consumer $consumer) {

  // Prepare some utils.
  $uri = $resource_object
    ->getField($resource_object
    ->getResourceType()
    ->getPublicName('uri'))->value;

  // Generate derivatives only for the found ones.
  $image_styles = $this->imageStylesProvider
    ->loadStyles($consumer);
  return array_reduce($image_styles, function (LinkCollection $decorated, ImageStyleInterface $image_style) use ($uri) {
    $variant_link = new Link(CacheableMetadata::createFromObject($image_style), Url::fromUri(file_create_url($image_style
      ->buildUrl($uri))), [
      ImageStylesProvider::DERIVATIVE_LINK_REL,
    ]);
    return $decorated
      ->withLink($image_style
      ->id(), $variant_link);
  }, (new LinkCollection([]))
    ->withContext($resource_object));
}