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