You are here

protected function ImageStyles::doUndoTransform in Consumer Image Styles 8.3

Same name and namespace in other branches
  1. 4.x src/Plugin/jsonapi/FieldEnhancer/ImageStyles.php \Drupal\consumer_image_styles\Plugin\jsonapi\FieldEnhancer\ImageStyles::doUndoTransform()

File

src/Plugin/jsonapi/FieldEnhancer/ImageStyles.php, line 79

Class

ImageStyles
Perform additional manipulations to timestamp fields.

Namespace

Drupal\consumer_image_styles\Plugin\jsonapi\FieldEnhancer

Code

protected function doUndoTransform($data, Context $context) {
  $image_style_ids = $this
    ->imageStylesForField();
  if (empty($image_style_ids)) {
    return $data;
  }

  // Load image style entities in bulk.
  try {
    $image_styles = $this->entityTypeManager
      ->getStorage('image_style')
      ->loadMultiple($image_style_ids);
  } catch (InvalidPluginDefinitionException $e) {
    $image_styles = [];
  }

  /** @var \Drupal\Core\Entity\Entity $entity */
  $uuid_key = $this->entityTypeManager
    ->getDefinition('file')
    ->getKey('uuid');
  $entities = $this->entityTypeManager
    ->getStorage('file')
    ->loadByProperties([
    $uuid_key => $data['id'],
  ]);
  $entity = reset($entities);

  // If the entity cannot be loaded or it's not an image, do not enhance it.
  if (!$entity || !$this->imageStylesProvider
    ->entityIsImage($entity)) {
    return $data;
  }

  /** @var \Drupal\file\Entity\File $entity */

  // If the entity is not viewable.
  $access = $entity
    ->access('view', NULL, TRUE);
  if (!$access
    ->isAllowed()) {
    return $data;
  }

  // @TODO: When enhanced transformations carry cacheable meta, add the access info.
  $uri = $entity
    ->getFileUri();
  $links = array_map(function (ImageStyleInterface $image_style) use ($uri) {
    return $this->imageStylesProvider
      ->buildDerivativeLink($uri, $image_style);
  }, $image_styles);

  // @TODO: When enhanced transformations carry cacheable meta, add the image styles entities.
  $meta = [
    'imageDerivatives' => [
      'links' => $links,
    ],
  ];
  return array_merge_recursive($data, [
    'meta' => $meta,
  ]);
}