You are here

ImageDerivative.php in GraphQL 8.4

File

src/Plugin/GraphQL/DataProducer/Entity/Fields/Image/ImageDerivative.php
View source
<?php

namespace Drupal\graphql\Plugin\GraphQL\DataProducer\Entity\Fields\Image;

use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\file\FileInterface;
use Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase;
use Drupal\image\Entity\ImageStyle;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns an image style derivative of an image.
 *
 * @DataProducer(
 *   id = "image_derivative",
 *   name = @Translation("Image Derivative"),
 *   description = @Translation("Returns an image derivative."),
 *   produces = @ContextDefinition("any",
 *     label = @Translation("Image derivative properties")
 *   ),
 *   consumes = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity"),
 *       required = FALSE
 *     ),
 *     "style" = @ContextDefinition("string",
 *       label = @Translation("Image style")
 *     )
 *   }
 * )
 */
class ImageDerivative extends DataProducerPluginBase implements ContainerFactoryPluginInterface {

  /**
   * The rendering service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * {@inheritdoc}
   *
   * @codeCoverageIgnore
   */
  public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
    return new static($configuration, $pluginId, $pluginDefinition, $container
      ->get('renderer'));
  }

  /**
   * ImageDerivative constructor.
   *
   * @param array $configuration
   *   The plugin configuration array.
   * @param string $pluginId
   *   The plugin id.
   * @param mixed $pluginDefinition
   *   The plugin definition.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   *
   * @codeCoverageIgnore
   */
  public function __construct(array $configuration, $pluginId, $pluginDefinition, RendererInterface $renderer) {
    parent::__construct($configuration, $pluginId, $pluginDefinition);
    $this->renderer = $renderer;
  }

  /**
   * Resolver.
   *
   * @param \Drupal\file\FileInterface $entity
   * @param string $style
   * @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
   *
   * @return array|null
   */
  public function resolve(FileInterface $entity = NULL, $style, RefinableCacheableDependencyInterface $metadata) {

    // Return if we dont have an entity.
    if (!$entity) {
      return NULL;
    }
    $access = $entity
      ->access('view', NULL, TRUE);
    $metadata
      ->addCacheableDependency($access);
    if ($access
      ->isAllowed() && ($image_style = ImageStyle::load($style))) {
      $width = $entity->width;
      $height = $entity->height;
      if (empty($width) || empty($height)) {

        /** @var \Drupal\Core\Image\ImageInterface $image */
        $image = \Drupal::service('image.factory')
          ->get($entity
          ->getFileUri());
        if ($image
          ->isValid()) {
          $width = $image
            ->getWidth();
          $height = $image
            ->getHeight();
        }
      }

      // Determine the dimensions of the styled image.
      $dimensions = [
        'width' => $width,
        'height' => $height,
      ];
      $image_style
        ->transformDimensions($dimensions, $entity
        ->getFileUri());
      $metadata
        ->addCacheableDependency($image_style);

      // The underlying URL generator that will be invoked will leak cache
      // metadata, resulting in an exception. By wrapping within a new render
      // context, we can capture the leaked metadata and make sure it gets
      // incorporated into the response.
      $context = new RenderContext();
      $url = $this->renderer
        ->executeInRenderContext($context, function () use ($image_style, $entity) {
        return $image_style
          ->buildUrl($entity
          ->getFileUri());
      });
      if (!$context
        ->isEmpty()) {
        $metadata
          ->addCacheableDependency($context
          ->pop());
      }
      return [
        'url' => $url,
        'width' => $dimensions['width'],
        'height' => $dimensions['height'],
      ];
    }
    return NULL;
  }

}

Classes

Namesort descending Description
ImageDerivative Returns an image style derivative of an image.