Skip to content

Commit 86e74d0

Browse files
blazeyofubhy
authored andcommitted
Add field for responsive image styles. (#350)
* #349 Added a field plugin and a test for the responsive image styles. * #349 Added DependencySerializationTrait to ImageResponsive. Adjusted ImageFieldTest. * #349 Addressed the code style change requests. * #349 Added ResponsiveImageStyleId enum. * #349 Only add the responsive field to iamges when the responsive_image module is enabled.
1 parent 77951fa commit 86e74d0

5 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Drupal\graphql_image\Plugin\Deriver;
4+
5+
use Drupal\Component\Plugin\Derivative\DeriverBase;
6+
use Drupal\Core\Extension\ModuleHandlerInterface;
7+
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8+
use Symfony\Component\DependencyInjection\ContainerInterface;
9+
10+
/**
11+
* Only add the 'responsive' field if the responsive_image module is enabled.
12+
*/
13+
class ImageResponsiveDeriver extends DeriverBase implements ContainerDeriverInterface {
14+
15+
/**
16+
* Entity type manager.
17+
*
18+
* @var \Drupal\Core\Extension\ModuleHandlerInterface
19+
*/
20+
protected $moduleHandler;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public static function create(ContainerInterface $container, $basePluginId) {
26+
return new static(
27+
$container->get('module_handler')
28+
);
29+
}
30+
31+
/**
32+
* EntityBundleDeriver constructor.
33+
*
34+
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
35+
* The module handler service.
36+
*/
37+
public function __construct(ModuleHandlerInterface $moduleHandler) {
38+
$this->moduleHandler = $moduleHandler;
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getDerivativeDefinitions($basePluginDefinition) {
45+
if ($this->moduleHandler->moduleExists('responsive_image')) {
46+
$this->derivatives['responsive_image'] = $basePluginDefinition;
47+
}
48+
return parent::getDerivativeDefinitions($basePluginDefinition);
49+
}
50+
51+
}
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_image\Plugin\GraphQL\Enums;
4+
5+
use Drupal\graphql_core\GraphQL\EnumPluginBase;
6+
use Drupal\graphql_core\GraphQLSchemaManagerInterface;
7+
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
8+
9+
/**
10+
* @GraphQLEnum(
11+
* id = "responsive_image_style_id",
12+
* name = "ResponsiveImageStyleId"
13+
* )
14+
*/
15+
class ResponsiveImageStyleId extends EnumPluginBase {
16+
17+
public function buildValues(GraphQLSchemaManagerInterface $schemaManager) {
18+
$items = [];
19+
foreach (ResponsiveImageStyle::loadMultiple() as $responsiveImageStyle) {
20+
$items[$responsiveImageStyle->id()] = [
21+
'value' => $responsiveImageStyle->id(),
22+
'name' => $responsiveImageStyle->id(),
23+
'description' => $responsiveImageStyle->label()
24+
];
25+
}
26+
return $items;
27+
}
28+
29+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Drupal\graphql_image\Plugin\GraphQL\Fields;
4+
5+
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
6+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7+
use Drupal\Core\Render\RendererInterface;
8+
use Drupal\graphql_core\GraphQL\FieldPluginBase;
9+
use Drupal\image\Plugin\Field\FieldType\ImageItem;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
use Youshido\GraphQL\Execution\ResolveInfo;
12+
13+
/**
14+
* Retrieve the responsive image.
15+
*
16+
* @GraphQLField(
17+
* id = "image_responsive",
18+
* secure = true,
19+
* name = "responsive",
20+
* type = "String",
21+
* nullable = true,
22+
* types = {"Image"},
23+
* arguments = {
24+
* "style" = "ResponsiveImageStyleId"
25+
* },
26+
* deriver = "Drupal\graphql_image\Plugin\Deriver\ImageResponsiveDeriver"
27+
* )
28+
*/
29+
class ImageResponsive extends FieldPluginBase implements ContainerFactoryPluginInterface {
30+
use DependencySerializationTrait;
31+
32+
/**
33+
* Renderer instance to render fields.
34+
*
35+
* @var \Drupal\Core\Render\RendererInterface
36+
*/
37+
protected $renderer;
38+
39+
/**
40+
* Constructs an ImageResponsive object.
41+
*
42+
* @param array $configuration
43+
* Plugin configuration.
44+
* @param string $pluginId
45+
* Id of the plugin.
46+
* @param array $pluginDefinition
47+
* Plugin definition array.
48+
* @param \Drupal\Core\Render\RendererInterface $renderer
49+
* The renderer service.
50+
*/
51+
public function __construct(array $configuration, $pluginId, $pluginDefinition, RendererInterface $renderer) {
52+
$this->renderer = $renderer;
53+
parent::__construct($configuration, $pluginId, $pluginDefinition);
54+
}
55+
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public static function create(ContainerInterface $container, array $configuration, $pluginId, $pluginDefinition) {
60+
return new static(
61+
$configuration,
62+
$pluginId,
63+
$pluginDefinition,
64+
$container->get('renderer')
65+
);
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
protected function resolveValues($value, array $args, ResolveInfo $info) {
72+
if ($value instanceof ImageItem && $value->entity->access('view')) {
73+
$variables = [
74+
'#theme' => 'responsive_image',
75+
'#responsive_image_style_id' => $args['style'],
76+
'#uri' => $value->uri,
77+
];
78+
79+
yield $this->renderer->renderRoot($variables);
80+
}
81+
}
82+
83+
}

modules/graphql_image/tests/queries/image.gql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ query ($path: String!) {
99
width
1010
height
1111
url
12+
responsive(style: style_one)
1213
thumbnailImage: derivative(style: thumbnail) {
1314
width
1415
height

modules/graphql_image/tests/src/Kernel/ImageFieldTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace Drupal\Tests\graphql_image\Kernel;
44

5+
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
56
use Drupal\Core\Entity\Entity\EntityViewDisplay;
67
use Drupal\Core\Entity\Entity\EntityViewMode;
78
use Drupal\field\Entity\FieldConfig;
89
use Drupal\field\Entity\FieldStorageConfig;
10+
use Drupal\responsive_image\Entity\ResponsiveImageStyle;
911
use Drupal\simpletest\ContentTypeCreationTrait;
1012
use Drupal\simpletest\NodeCreationTrait;
1113
use Drupal\Tests\graphql_core\Kernel\GraphQLFileTestBase;
@@ -30,6 +32,8 @@ class ImageFieldTest extends GraphQLFileTestBase {
3032
'filter',
3133
'file',
3234
'image',
35+
'breakpoint',
36+
'responsive_image',
3337
'graphql_content',
3438
'graphql_file',
3539
'graphql_image',
@@ -92,6 +96,17 @@ protected function setUp() {
9296
],
9397
])
9498
->save();
99+
100+
$responsiveImgStyle = ResponsiveImageStyle::create(array(
101+
'id' => 'style_one',
102+
'label' => 'Style One',
103+
'breakpoint_group' => 'graphql_image',
104+
));
105+
$responsiveImgStyle->addImageStyleMapping('graphql_image.mobile', '1x', array(
106+
'image_mapping_type' => 'image_style',
107+
'image_mapping' => 'thumbnail',
108+
));
109+
$responsiveImgStyle->save();
95110
}
96111

97112
/**
@@ -113,6 +128,7 @@ public function testImageField() {
113128
$this->assertEquals($a->image->alt, $image['alt'], 'Alt text correct.');
114129
$this->assertEquals($a->image->title, $image['title'], 'Title text correct.');
115130
$this->assertEquals($a->image->entity->url(), $image['url'], 'Retrieve correct image url.');
131+
$this->assertNotEmpty(trim($image['responsive']), 'Responsive image returned.');
116132
}
117133

118134
}

0 commit comments

Comments
 (0)