1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image; |
4
|
|
|
|
5
|
|
|
use Drupal\Core\DependencyInjection\DependencySerializationTrait; |
6
|
|
|
use Drupal\Core\Image\ImageFactory; |
7
|
|
|
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
8
|
|
|
use Drupal\graphql\GraphQL\Cache\CacheableValue; |
9
|
|
|
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase; |
10
|
|
|
use Drupal\image\Plugin\Field\FieldType\ImageItem; |
11
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
12
|
|
|
use Youshido\GraphQL\Execution\ResolveInfo; |
13
|
|
|
use Drupal\image\Entity\ImageStyle; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Retrieve the image field derivative (image style). |
17
|
|
|
* |
18
|
|
|
* @GraphQLField( |
19
|
|
|
* id = "image_derivative", |
20
|
|
|
* secure = true, |
21
|
|
|
* name = "derivative", |
22
|
|
|
* type = "ImageResource", |
23
|
|
|
* arguments = { |
24
|
|
|
* "style" = "ImageStyleId!" |
25
|
|
|
* }, |
26
|
|
|
* field_types = {"image"}, |
27
|
|
|
* provider = "image", |
28
|
|
|
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver" |
29
|
|
|
* ) |
30
|
|
|
*/ |
31
|
|
|
class ImageDerivative extends FieldPluginBase implements ContainerFactoryPluginInterface { |
|
|
|
|
32
|
|
|
|
33
|
|
|
use DependencySerializationTrait; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The image factory. |
37
|
|
|
* |
38
|
|
|
* @var \Drupal\Core\Image\ImageFactory |
39
|
|
|
*/ |
40
|
|
|
protected $imageFactory; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $configuration, $pluginId, $pluginDefinition, ImageFactory $imageFactory) { |
46
|
|
|
parent::__construct($configuration, $pluginId, $pluginDefinition); |
47
|
|
|
$this->imageFactory = $imageFactory; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) { |
54
|
|
|
return new static($configuration, $pluginId, $pluginDefinition, $container->get('image.factory')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
|
protected function resolveValues($value, array $args, ResolveInfo $info) { |
61
|
|
|
if ($value instanceof ImageItem && $value->entity->access('view') && $style = ImageStyle::load($args['style'])) { |
|
|
|
|
62
|
|
|
$file = $value->entity; |
63
|
|
|
|
64
|
|
|
// Determine the dimensions of the styled image. |
65
|
|
|
$dimensions = [ |
66
|
|
|
'width' => $value->width, |
67
|
|
|
'height' => $value->height, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
$style->transformDimensions($dimensions, $file->getFileUri()); |
71
|
|
|
|
72
|
|
|
yield new CacheableValue([ |
73
|
|
|
'url' => $style->buildUrl($file->getFileUri()), |
74
|
|
|
'width' => $dimensions['width'], |
75
|
|
|
'height' => $dimensions['height'], |
76
|
|
|
], [$style]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|