You are here

class ImageStylesProvider in Consumer Image Styles 4.x

Same name and namespace in other branches
  1. 8.3 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider
  2. 8.2 src/ImageStylesProvider.php \Drupal\consumer_image_styles\ImageStylesProvider

Class ImageStylesProvider.

@package Drupal\consumer_image_styles

Hierarchy

Expanded class hierarchy of ImageStylesProvider

2 files declare their use of ImageStylesProvider
ConsumerImageSylesFunctionalTest.php in tests/src/Functional/ConsumerImageSylesFunctionalTest.php
LinkCollectionNormalizer.php in src/Normalizer/LinkCollectionNormalizer.php
1 string reference to 'ImageStylesProvider'
consumer_image_styles.services.yml in ./consumer_image_styles.services.yml
consumer_image_styles.services.yml
1 service uses ImageStylesProvider
consumer_image_styles.image_styles_provider in ./consumer_image_styles.services.yml
Drupal\consumer_image_styles\ImageStylesProvider

File

src/ImageStylesProvider.php, line 19

Namespace

Drupal\consumer_image_styles
View source
class ImageStylesProvider implements ImageStylesProviderInterface {
  use StringTranslationTrait;
  const DERIVATIVE_LINK_REL = 'drupal://jsonapi/extensions/consumer_image_styles/links/relation-types/#derivative';

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

  /**
   * The image factory.
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  private $imageFactory;
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ImageFactory $image_factory) {
    $this->entityTypeManager = $entity_type_manager;
    $this->imageFactory = $image_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function loadStyles(Consumer $consumer) {
    $consumer_config = $consumer
      ->get('image_styles')
      ->getValue();
    $image_style_ids = array_map(function ($field_value) {
      return $field_value['target_id'];
    }, $consumer_config);

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

  /**
   * {@inheritdoc}
   */
  public function buildDerivativeLink($uri, ImageStyleInterface $image_style) {
    $image = $this->imageFactory
      ->get($uri);
    $dimensions = [
      'width' => $image
        ->getWidth(),
      'height' => $image
        ->getHeight(),
    ];
    $image_style
      ->transformDimensions($dimensions, $uri);
    return [
      'href' => file_create_url($image_style
        ->buildUrl($uri)),
      'title' => $this
        ->t('Image Style: @name', [
        '@name' => $image_style
          ->label(),
      ]),
      'type' => $image
        ->getMimeType(),
      'meta' => [
        'rel' => static::DERIVATIVE_LINK_REL,
      ] + $dimensions,
    ];
  }

  /**
   * {inheritdoc}
   */
  public function entityIsImage(EntityInterface $entity) {
    if (!$entity instanceof File) {
      return FALSE;
    }
    return in_array(mb_strtolower(pathinfo($entity
      ->getFileUri(), PATHINFO_EXTENSION)), $this->imageFactory
      ->getSupportedExtensions());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ImageStylesProvider::$entityTypeManager private property The entity type manager.
ImageStylesProvider::$imageFactory private property The image factory.
ImageStylesProvider::buildDerivativeLink public function Builds a derivative link based on the image URI and the image style. Overrides ImageStylesProviderInterface::buildDerivativeLink
ImageStylesProvider::DERIVATIVE_LINK_REL constant
ImageStylesProvider::entityIsImage public function {inheritdoc} Overrides ImageStylesProviderInterface::entityIsImage
ImageStylesProvider::loadStyles public function Load the image styles for a given consumer. Overrides ImageStylesProviderInterface::loadStyles
ImageStylesProvider::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.