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;
class ImageUrl extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
protected $renderer;
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('renderer'));
}
public function __construct(array $configuration, $pluginId, $pluginDefinition, RendererInterface $renderer) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->renderer = $renderer;
}
public function resolve(FileInterface $entity, RefinableCacheableDependencyInterface $metadata) {
$access = $entity
->access('view', NULL, TRUE);
$metadata
->addCacheableDependency($access);
if ($access
->isAllowed()) {
$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
Name |
Description |
ImageUrl |
Returns the file URL of a file entity. |