You are here

ImageUrl.php in GraphQL 8.4

File

src/Plugin/GraphQL/DataProducer/Entity/Fields/Image/ImageUrl.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 Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Returns the file URL of a file entity.
 *
 * @DataProducer(
 *   id = "image_url",
 *   name = @Translation("Image URL"),
 *   description = @Translation("Returns the url of an image entity."),
 *   produces = @ContextDefinition("string",
 *     label = @Translation("URL")
 *   ),
 *   consumes = {
 *     "entity" = @ContextDefinition("entity",
 *       label = @Translation("Entity")
 *     )
 *   }
 * )
 */
class ImageUrl 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'));
  }

  /**
   * ImageUrl 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 \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
   *
   * @return string|null
   */
  public function resolve(FileInterface $entity, RefinableCacheableDependencyInterface $metadata) {
    $access = $entity
      ->access('view', NULL, TRUE);
    $metadata
      ->addCacheableDependency($access);
    if ($access
      ->isAllowed()) {

      // 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 ($entity) {
        return file_create_url($entity
          ->getFileUri());
      });
      if (!$context
        ->isEmpty()) {
        $metadata
          ->addCacheableDependency($context
          ->pop());
      }
      return $url;
    }
    return NULL;
  }

}

Classes

Namesort descending Description
ImageUrl Returns the file URL of a file entity.