-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathImageDerivative.php
158 lines (141 loc) · 4.59 KB
/
ImageDerivative.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?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\file_entity\FileEntityInterface;
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;
// Optionally support the extra data from file_entity.
if ($entity instanceof FileEntityInterface) {
if (empty($width)) {
$width = $entity->getMetadata('width');
}
if (empty($height)) {
$height = $entity->getMetadata('height');
}
}
// @todo Not sure why PHPStan complains here, this should be refactored to
// check the entity properties first.
// @phpstan-ignore-next-line
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;
}
}