You are here

class ImageEntityNormalizer in Consumer Image Styles 8

Same name and namespace in other branches
  1. 8.2 src/Normalizer/ImageEntityNormalizer.php \Drupal\consumer_image_styles\Normalizer\ImageEntityNormalizer

Hierarchy

Expanded class hierarchy of ImageEntityNormalizer

1 string reference to 'ImageEntityNormalizer'
consumer_image_styles.services.yml in ./consumer_image_styles.services.yml
consumer_image_styles.services.yml
1 service uses ImageEntityNormalizer
serializer.normalizer.image_entity.consumer_image_styles in ./consumer_image_styles.services.yml
Drupal\consumer_image_styles\Normalizer\ImageEntityNormalizer

File

src/Normalizer/ImageEntityNormalizer.php, line 16

Namespace

Drupal\consumer_image_styles\Normalizer
View source
class ImageEntityNormalizer extends ContentEntityNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = File::class;

  /**
   * The formats that the Normalizer can handle.
   *
   * @var array
   */
  protected $formats = [
    'api_json',
  ];

  /**
   * The link manager.
   *
   * @var \Drupal\jsonapi\LinkManager\LinkManager
   */
  protected $linkManager;

  /**
   * The JSON API resource type repository.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
   */
  protected $resourceTypeRepository;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @var \Drupal\consumers\Negotiator
   */
  protected $consumerNegotiator;

  /**
   * Constructs an EntityNormalizer object.
   *
   * @param \Drupal\jsonapi\LinkManager\LinkManager $link_manager
   *   The link manager.
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
   *   The JSON API resource type repository.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\consumers\Negotiator $consumer_negotiator
   *   The consumer negotiator.
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function supportsNormalization($data, $format = NULL) {

    // It is very tricky to detect if a file entity is an image or not. This is
    // typically done using a special field type to point to this entity.
    // However we don't have access to that in here. Besides we want this to
    // apply when requesting a listing of file entities as well, not only via
    // includes. For all this we'll do string matching against the mimetype.
    return parent::supportsNormalization($data, $format) && strpos($data
      ->get('filemime')->value, 'image/') !== FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function supportsDenormalization($data, $type, $format = NULL) {

    // We do not need to do anything special about denormalization. Passing here
    // will have the serializer use the normal content entity normalizer.
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {

    // This should never be called.
    throw new \Exception('Unsupported denormalizer.');
  }

  /**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   * @param array $context
   *
   * @return \Drupal\jsonapi\Normalizer\Value\ValueExtractorInterface
   */
  protected function buildVariantValues(EntityInterface $entity, array $context = []) {
    $request = empty($context['request']) ? NULL : $context['request'];
    $consumer = $this->consumerNegotiator
      ->negotiateFromRequest($request);
    $output = new NullFieldNormalizerValue();
    if ($consumer) {

      // Get the image styles from the 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) {

        /** @var \Drupal\image\Entity\ImageStyle $image_style */
        $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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
EntityNormalizer::getFieldItemInstance protected function Gets a field item instance for use with SerializedColumnNormalizerTrait.
EntityNormalizer::getFieldNames protected function Gets the field names for the given entity.
EntityNormalizer::getFields protected function Gets the field names for the given entity. 1
EntityNormalizer::prepareInput protected function Prepares the input data to create the entity.
EntityNormalizer::serializeField protected function Serializes a given field. 1
ImageEntityNormalizer::$consumerNegotiator protected property
ImageEntityNormalizer::$entityTypeManager protected property The entity type manager. Overrides EntityNormalizer::$entityTypeManager
ImageEntityNormalizer::$formats protected property The formats that the Normalizer can handle. Overrides EntityNormalizer::$formats
ImageEntityNormalizer::$linkManager protected property The link manager. Overrides EntityNormalizer::$linkManager
ImageEntityNormalizer::$resourceTypeRepository protected property The JSON API resource type repository. Overrides EntityNormalizer::$resourceTypeRepository
ImageEntityNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides EntityNormalizer::$supportedInterfaceOrClass
ImageEntityNormalizer::buildVariantValues protected function
ImageEntityNormalizer::denormalize public function Denormalizes data back into an object of the given class. Overrides EntityNormalizer::denormalize
ImageEntityNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides EntityNormalizer::normalize
ImageEntityNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
ImageEntityNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization
ImageEntityNormalizer::__construct public function Constructs an EntityNormalizer object. Overrides EntityNormalizer::__construct
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
SerializedColumnNormalizerTrait::checkForSerializedStrings protected function Checks if there is a serialized string for a column.
SerializedColumnNormalizerTrait::dataHasStringForSerializeColumn protected function Checks if the data contains string value for serialize column.
SerializedColumnNormalizerTrait::getCustomSerializedPropertyNames protected function Gets the names of all properties the plugin treats as serialized data.
SerializedColumnNormalizerTrait::getSerializedPropertyNames protected function Gets the names of all serialized properties.