Commit c3596555 authored by Sascha Grossenbacher's avatar Sascha Grossenbacher Committed by Julian Pustkuchen
Browse files

Issue #3132556: Do we need to call the toolkit to get image dimensions from...

Issue #3132556: Do we need to call the toolkit to get image dimensions from the file when they are available as image field attributes?
parent fd1df2c8
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -113,6 +113,10 @@ class ImageDTO {

    $this->uri = $this->item->entity->getFileUri();
    $this->entity = $variables['entity'];
    $this->setDimensions([
      ImageDTO::HEIGHT => $item->height,
      ImageDTO::WIDTH => $item->width,
    ]);
  }

  /**
+2 −32
Original line number Diff line number Diff line
@@ -23,13 +23,6 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
   */
  protected $entityTypeManager;

  /**
   * Image factory.
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;

  /**
   * Token.
   *
@@ -70,8 +63,6 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   Entity manager.
   * @param \Drupal\Core\Image\ImageFactory $imageFactory
   *   Image factory.
   * @param \Drupal\Core\Utility\Token $token
   *   Token.
   * @param \Drupal\Core\Language\LanguageManagerInterface $languageManager
@@ -83,14 +74,12 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
   */
  public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    ImageFactory $imageFactory,
    Token $token,
    LanguageManagerInterface $languageManager,
    LoggerChannelFactoryInterface $channelFactory,
    RendererInterface $renderer
  ) {
    $this->entityTypeManager = $entityTypeManager;
    $this->imageFactory = $imageFactory;
    $this->token = $token;
    $this->languageManager = $languageManager;
    $this->renderer = $renderer;
@@ -103,7 +92,6 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
      $container->get('image.factory'),
      $container->get('token'),
      $container->get('language_manager'),
      $container->get('logger.factory'),
@@ -120,7 +108,6 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
  public function preprocess(array &$variables) {
    $this->imageDTO = ImageDTO::createFromVariables($variables);
    $image = $this->getRandarableImage($variables);
    $this->setDimensions($image);

    $variables['image'] = $image;
    $variables['path'] = $this->getPath();
@@ -214,6 +201,8 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
      '#uri' => $this->imageDTO->getUri(),
      '#alt' => $this->imageDTO->getAlt(),
      '#title' => $this->imageDTO->getTitle(),
      '#width' => $this->imageDTO->getWidth(),
      '#height' => $this->imageDTO->getHeight(),
      '#attributes' => $this->imageDTO->getItem()->_attributes,
      '#style_name' => $this->imageDTO->getSettings()['photoswipe_node_style'],
    ];
@@ -252,23 +241,4 @@ class PhotoswipePreprocessProcessor implements ContainerInjectionInterface {
    }
  }

  /**
   * Set image dimensions.
   *
   * @param array $image
   *   Randarable array of image.
   */
  protected function setDimensions(array &$image) {
    // The image.factory service will check if our image is valid.
    $image_file = $this->imageFactory->get($this->imageDTO->getUri());
    if ($image_file->isValid()) {
      $image['#width'] = $image_file->getWidth();
      $image['#height'] = $image_file->getHeight();
      $this->imageDTO->setDimensions([
        ImageDTO::HEIGHT => $image_file->getHeight(),
        ImageDTO::WIDTH => $image_file->getWidth(),
      ]);
    }
  }

}