Skip to content

Commit 1297116

Browse files
committed
Migrated image derivatives to graphql_core.
1 parent 9e14ee2 commit 1297116

9 files changed

Lines changed: 357 additions & 1 deletion

File tree

modules/graphql_core/src/Plugin/Deriver/Fields/EntityFieldPropertyDeriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public function getDerivativeDefinitions($basePluginDefinition) {
8484
}
8585

8686
$this->derivatives[$basePluginDefinition['id']] = [
87-
'parents' => $parents,
87+
'parents' => array_merge($parents, $basePluginDefinition['parents']),
8888
] + $basePluginDefinition;
8989

9090
return $this->derivatives;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Enums\Images;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Enums\EnumPluginBase;
6+
use Drupal\graphql\Plugin\GraphQL\PluggableSchemaManagerInterface;
7+
use Drupal\image\Entity\ImageStyle as ImageStyleConfig;
8+
9+
/**
10+
* @GraphQLEnum(
11+
* id = "image_style_id",
12+
* name = "ImageStyleId"
13+
* )
14+
*/
15+
class ImageStyleId extends EnumPluginBase {
16+
17+
public function buildValues(PluggableSchemaManagerInterface $schemaManager) {
18+
$items = [];
19+
foreach (ImageStyleConfig::loadMultiple() as $imageStyle) {
20+
$items[$imageStyle->id()] = [
21+
'value' => $imageStyle->id(),
22+
'name' => $imageStyle->id(),
23+
'description' => $imageStyle->label()
24+
];
25+
}
26+
return $items;
27+
}
28+
29+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
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+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Images;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6+
use Drupal\image\Plugin\Field\FieldType\ImageItem;
7+
use Youshido\GraphQL\Execution\ResolveInfo;
8+
9+
/**
10+
* Retrieve the image height.
11+
*
12+
* @GraphQLField(
13+
* id = "image_style_height",
14+
* secure = true,
15+
* name = "height",
16+
* type = "Int",
17+
* nullable = true,
18+
* parents = {"ImageResource"},
19+
* field_types = {"image"},
20+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
21+
* )
22+
*/
23+
class ImageResourceHeight extends FieldPluginBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function resolveValues($value, array $args, ResolveInfo $info) {
29+
if ($value instanceof ImageItem && $value->entity->access('view')) {
30+
yield (int) $value->height;
31+
}
32+
if (is_array($value) && array_key_exists('height', $value)) {
33+
yield (int) $value['height'];
34+
}
35+
}
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Images;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6+
use Drupal\image\Plugin\Field\FieldType\ImageItem;
7+
use Youshido\GraphQL\Execution\ResolveInfo;
8+
9+
/**
10+
* Retrieve the image url.
11+
*
12+
* @GraphQLField(
13+
* id = "image_style_url",
14+
* secure = true,
15+
* name = "url",
16+
* type = "String",
17+
* nullable = true,
18+
* parents = {"ImageResource"},
19+
* field_types = {"image"},
20+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
21+
* )
22+
*/
23+
class ImageResourceUrl extends FieldPluginBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function resolveValues($value, array $args, ResolveInfo $info) {
29+
if ($value instanceof ImageItem && $value->entity->access('view')) {
30+
yield file_create_url($value->entity->getFileUri());
31+
}
32+
if (is_array($value) && array_key_exists('url', $value)) {
33+
yield $value['url'];
34+
}
35+
}
36+
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Images;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
6+
use Drupal\image\Plugin\Field\FieldType\ImageItem;
7+
use Youshido\GraphQL\Execution\ResolveInfo;
8+
9+
/**
10+
* Retrieve the image width.
11+
*
12+
* @GraphQLField(
13+
* id = "image_style_width",
14+
* secure = true,
15+
* name = "width",
16+
* type = "Int",
17+
* nullable = true,
18+
* parents = {"ImageResource"},
19+
* field_types = {"image"},
20+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
21+
* )
22+
*/
23+
class ImageResourceWidth extends FieldPluginBase {
24+
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
protected function resolveValues($value, array $args, ResolveInfo $info) {
29+
if ($value instanceof ImageItem && $value->entity->access('view')) {
30+
yield (int) $value->width;
31+
}
32+
if (is_array($value) && array_key_exists('width', $value)) {
33+
yield (int) $value['width'];
34+
}
35+
}
36+
37+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Types\Images;
4+
5+
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
6+
7+
/**
8+
* @GraphQLType(
9+
* id = "image_resource",
10+
* name = "ImageResource"
11+
* )
12+
*/
13+
class ImageResource extends TypePluginBase {
14+
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
query ($path: String!) {
2+
route:route(path: $path) {
3+
node:entity {
4+
image {
5+
alt
6+
title
7+
width
8+
height
9+
entity {
10+
url
11+
}
12+
thumbnailImage:derivative(style: thumbnail) {
13+
width
14+
height
15+
url
16+
}
17+
}
18+
}
19+
}
20+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
<?php
2+
3+
namespace Drupal\Tests\graphql_core\Kernel\Images;
4+
5+
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6+
use Drupal\Core\Entity\Entity\EntityViewDisplay;
7+
use Drupal\Core\Entity\Entity\EntityViewMode;
8+
use Drupal\field\Entity\FieldConfig;
9+
use Drupal\field\Entity\FieldStorageConfig;
10+
use Drupal\image\Entity\ImageStyle;
11+
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
12+
use Drupal\simpletest\ContentTypeCreationTrait;
13+
use Drupal\simpletest\NodeCreationTrait;
14+
use Drupal\Tests\graphql\Kernel\GraphQLFileTestBase;
15+
use Drupal\user\Entity\Role;
16+
17+
/**
18+
* Test file attachments.
19+
*
20+
* @group graphql_image
21+
*/
22+
class ImageFieldTest extends GraphQLFileTestBase {
23+
use NodeCreationTrait;
24+
use ContentTypeCreationTrait;
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public static $modules = [
30+
'node',
31+
'field',
32+
'text',
33+
'filter',
34+
'file',
35+
'image',
36+
'graphql_core',
37+
];
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
protected function setUp() {
43+
parent::setUp();
44+
$this->installConfig('node');
45+
$this->installConfig('filter');
46+
$this->installConfig('image');
47+
$this->installEntitySchema('node');
48+
$this->installSchema('node', 'node_access');
49+
$this->installSchema('file', 'file_usage');
50+
$this->installEntitySchema('file');
51+
$this->createContentType(['type' => 'test']);
52+
53+
Role::load('anonymous')
54+
->grantPermission('access content')
55+
->save();
56+
57+
EntityViewMode::create([
58+
'targetEntityType' => 'node',
59+
'id' => "node.graphql",
60+
])->save();
61+
62+
63+
FieldStorageConfig::create([
64+
'field_name' => 'image',
65+
'type' => 'image',
66+
'entity_type' => 'node',
67+
])->save();
68+
69+
FieldConfig::create([
70+
'field_name' => 'image',
71+
'entity_type' => 'node',
72+
'bundle' => 'test',
73+
'label' => 'Image',
74+
])->save();
75+
76+
}
77+
78+
/**
79+
* Test a simple file field.
80+
*/
81+
public function testImageField() {
82+
$a = $this->createNode([
83+
'title' => 'Node A',
84+
'type' => 'test',
85+
]);
86+
87+
$a->image->generateSampleItems(1);
88+
89+
$a->save();
90+
91+
$result = $this->executeQueryFile('image.gql', ['path' => '/node/' . $a->id()]);
92+
$image = $result['data']['route']['node']['image'];
93+
94+
$this->assertEquals($a->image->alt, $image['alt'], 'Alt text correct.');
95+
$this->assertEquals($a->image->title, $image['title'], 'Title text correct.');
96+
$this->assertEquals($a->image->entity->url(), $image['entity']['url'], 'Retrieve correct image url.');
97+
$imageStyle = ImageStyle::load('thumbnail');
98+
$styleUrl = $imageStyle->buildUrl($a->image->entity->uri->value);
99+
$this->assertEquals($styleUrl, $image['thumbnailImage']['url']);
100+
}
101+
102+
}

0 commit comments

Comments
 (0)