class ThunderImage in Thunder 6.2.x
Returns image meta data.
Plugin annotation
@DataProducer(
id = "thunder_image",
name = @Translation("Image meta data"),
description = @Translation("Returns the meta data of an image entity."),
produces = @ContextDefinition("any",
label = @Translation("Metadata")
),
consumes = {
"entity" = @ContextDefinition("entity",
label = @Translation("Entity")
),
"field" = @ContextDefinition("any",
label = @Translation("Field")
)
}
)
Hierarchy
- class \Drupal\thunder_gqls\Plugin\GraphQL\DataProducer\ThunderImage extends \Drupal\graphql\Plugin\GraphQL\DataProducer\DataProducerPluginBase implements ContainerFactoryPluginInterface
Expanded class hierarchy of ThunderImage
File
- modules/
thunder_gqls/ src/ Plugin/ GraphQL/ DataProducer/ ThunderImage.php, line 34
Namespace
Drupal\thunder_gqls\Plugin\GraphQL\DataProducerView source
class ThunderImage extends DataProducerPluginBase implements ContainerFactoryPluginInterface {
/**
* The rendering service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* The image factory.
*
* @var \Drupal\Core\Image\ImageFactory
*/
protected $imageFactory;
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
return new static($configuration, $pluginId, $pluginDefinition, $container
->get('renderer'), $container
->get('image.factory'));
}
/**
* 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.
* @param \Drupal\Core\Image\ImageFactory $imageFactory
* The image factory.
*
* @codeCoverageIgnore
*/
public function __construct(array $configuration, $pluginId, $pluginDefinition, RendererInterface $renderer, ImageFactory $imageFactory) {
parent::__construct($configuration, $pluginId, $pluginDefinition);
$this->renderer = $renderer;
$this->imageFactory = $imageFactory;
}
/**
* Resolver.
*
* @param \Drupal\file\FileInterface $entity
* The file entity.
* @param array $field
* Values of the field.
* @param \Drupal\Core\Cache\RefinableCacheableDependencyInterface $metadata
* The cacheable dependency interface.
*
* @return array
* The image meta data
*/
public function resolve(FileInterface $entity, array $field, RefinableCacheableDependencyInterface $metadata) {
$access = $entity
->access('view', NULL, TRUE);
$metadata
->addCacheableDependency($access);
if ($access
->isAllowed()) {
$context = new RenderContext();
$imageFactory = $this->imageFactory;
$data = $this->renderer
->executeInRenderContext($context, function () use ($entity, $imageFactory, $field) {
$uri = $entity
->getFileUri();
$image = $imageFactory
->get($uri);
$current_field = reset($field);
return [
'src' => file_create_url($uri),
'width' => $image
->getWidth(),
'height' => $image
->getHeight(),
'alt' => $current_field['alt'],
'title' => $current_field['title'],
];
});
if (!$context
->isEmpty()) {
$metadata
->addCacheableDependency($context
->pop());
}
return $data;
}
return [];
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ThunderImage:: |
protected | property | The image factory. | |
ThunderImage:: |
protected | property | The rendering service. | |
ThunderImage:: |
public static | function |
@codeCoverageIgnore Overrides ContainerFactoryPluginInterface:: |
|
ThunderImage:: |
public | function | Resolver. | |
ThunderImage:: |
public | function | ImageUrl constructor. |