ImageEntityNormalizer.php in Consumer Image Styles 8
File
src/Normalizer/ImageEntityNormalizer.php
View source
<?php
namespace Drupal\consumer_image_styles\Normalizer;
use Drupal\consumer_image_styles\Normalizer\Value\ImageVariantItemNormalizerValue;
use Drupal\consumer_image_styles\Normalizer\Value\ImageNormalizerValue;
use Drupal\consumers\Negotiator;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\Entity\File;
use Drupal\jsonapi\LinkManager\LinkManager;
use Drupal\jsonapi\Normalizer\ContentEntityNormalizer;
use Drupal\jsonapi\Normalizer\Value\NullFieldNormalizerValue;
use Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface;
class ImageEntityNormalizer extends ContentEntityNormalizer {
protected $supportedInterfaceOrClass = File::class;
protected $formats = [
'api_json',
];
protected $linkManager;
protected $resourceTypeRepository;
protected $entityTypeManager;
protected $consumerNegotiator;
public function __construct(LinkManager $link_manager, ResourceTypeRepositoryInterface $resource_type_repository, EntityTypeManagerInterface $entity_type_manager, Negotiator $consumer_negotiator) {
$this->linkManager = $link_manager;
$this->resourceTypeRepository = $resource_type_repository;
$this->entityTypeManager = $entity_type_manager;
$this->consumerNegotiator = $consumer_negotiator;
parent::__construct($link_manager, $resource_type_repository, $entity_type_manager);
}
public function supportsNormalization($data, $format = NULL) {
return parent::supportsNormalization($data, $format) && strpos($data
->get('filemime')->value, 'image/') !== FALSE;
}
public function supportsDenormalization($data, $type, $format = NULL) {
return FALSE;
}
public function normalize($entity, $format = NULL, array $context = []) {
$file_entity_values = parent::normalize($entity, $format, $context);
$variants = $this
->buildVariantValues($entity, $context);
return new ImageNormalizerValue($variants, $file_entity_values);
}
public function denormalize($data, $class, $format = NULL, array $context = []) {
throw new \Exception('Unsupported denormalizer.');
}
protected function buildVariantValues(EntityInterface $entity, array $context = []) {
$request = empty($context['request']) ? NULL : $context['request'];
$consumer = $this->consumerNegotiator
->negotiateFromRequest($request);
$output = new NullFieldNormalizerValue();
if ($consumer) {
$image_style_ids = array_map(function ($field_value) {
return $field_value['target_id'];
}, $consumer
->get('image_styles')
->getValue());
$uri = $entity
->get('uri')->value;
$image_style_storage = $this->entityTypeManager
->getStorage('image_style');
$urls = array_map(function ($image_style_id) use ($image_style_storage, $uri) {
$image_style = $image_style_storage
->load($image_style_id);
return file_create_url($image_style
->buildUrl($uri));
}, $image_style_ids);
$value = array_combine($image_style_ids, $urls);
$output = new ImageVariantItemNormalizerValue($value);
$output
->addCacheableDependency($consumer);
}
return $output;
}
}