Skip to content

Commit e0d73dd

Browse files
authored
Re-organize image fields. (#508)
1 parent e1cafb0 commit e0d73dd

7 files changed

Lines changed: 106 additions & 32 deletions

File tree

modules/graphql_core/src/Plugin/GraphQL/Fields/Images/ImageDerivative.php renamed to modules/graphql_core/src/Plugin/GraphQL/Fields/Entity/Fields/Image/ImageDerivative.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<?php
22

3-
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Images;
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
44

5-
use Drupal\Core\Cache\Cache;
65
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
76
use Drupal\Core\Image\ImageFactory;
87
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
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_height",
14+
* secure = true,
15+
* name = "height",
16+
* type = "Int",
17+
* provider = "image",
18+
* field_types = {"image"},
19+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
20+
* )
21+
*/
22+
class ImageHeight extends FieldPluginBase {
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function resolveValues($value, array $args, ResolveInfo $info) {
28+
if ($value instanceof ImageItem && $value->entity->access('view')) {
29+
yield (int) $value->height;
30+
}
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
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_url",
14+
* secure = true,
15+
* name = "url",
16+
* type = "String",
17+
* field_types = {"image"},
18+
* provider = "image",
19+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
20+
* )
21+
*/
22+
class ImageUrl extends FieldPluginBase {
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function resolveValues($value, array $args, ResolveInfo $info) {
28+
if ($value instanceof ImageItem && $value->entity->access('view')) {
29+
yield file_create_url($value->entity->getFileUri());
30+
}
31+
}
32+
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Drupal\graphql_core\Plugin\GraphQL\Fields\Entity\Fields\Image;
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_width",
14+
* secure = true,
15+
* name = "width",
16+
* type = "Int",
17+
* provider = "image",
18+
* field_types = {"image"},
19+
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
20+
* )
21+
*/
22+
class ImageWidth extends FieldPluginBase {
23+
24+
/**
25+
* {@inheritdoc}
26+
*/
27+
protected function resolveValues($value, array $args, ResolveInfo $info) {
28+
if ($value instanceof ImageItem && $value->entity->access('view')) {
29+
yield (int) $value->width;
30+
}
31+
}
32+
33+
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Images/ImageResourceHeight.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
* secure = true,
1515
* name = "height",
1616
* type = "Int",
17-
* provider = "image",
1817
* parents = {"ImageResource"},
19-
* field_types = {"image"},
20-
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
18+
* provider = "image"
2119
* )
2220
*/
2321
class ImageResourceHeight extends FieldPluginBase {
@@ -26,13 +24,7 @@ class ImageResourceHeight extends FieldPluginBase {
2624
* {@inheritdoc}
2725
*/
2826
protected function resolveValues($value, array $args, ResolveInfo $info) {
29-
if ($value instanceof ImageItem && $value->entity->access('view')) {
30-
yield (int) $value->height;
31-
}
32-
33-
if (is_array($value) && array_key_exists('height', $value)) {
34-
yield (int) $value['height'];
35-
}
27+
yield (int) $value['height'];
3628
}
3729

3830
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Images/ImageResourceUrl.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
* name = "url",
1616
* type = "String",
1717
* parents = {"ImageResource"},
18-
* field_types = {"image"},
19-
* provider = "image",
20-
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
18+
* provider = "image"
2119
* )
2220
*/
2321
class ImageResourceUrl extends FieldPluginBase {
@@ -26,13 +24,7 @@ class ImageResourceUrl extends FieldPluginBase {
2624
* {@inheritdoc}
2725
*/
2826
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-
33-
if (is_array($value) && array_key_exists('url', $value)) {
34-
yield $value['url'];
35-
}
27+
yield $value['url'];
3628
}
3729

3830
}

modules/graphql_core/src/Plugin/GraphQL/Fields/Images/ImageResourceWidth.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
* secure = true,
1515
* name = "width",
1616
* type = "Int",
17-
* provider = "image",
1817
* parents = {"ImageResource"},
19-
* field_types = {"image"},
20-
* deriver = "Drupal\graphql_core\Plugin\Deriver\Fields\EntityFieldPropertyDeriver"
18+
* provider = "image"
2119
* )
2220
*/
2321
class ImageResourceWidth extends FieldPluginBase {
@@ -26,13 +24,7 @@ class ImageResourceWidth extends FieldPluginBase {
2624
* {@inheritdoc}
2725
*/
2826
protected function resolveValues($value, array $args, ResolveInfo $info) {
29-
if ($value instanceof ImageItem && $value->entity->access('view')) {
30-
yield (int) $value->width;
31-
}
32-
33-
if (is_array($value) && array_key_exists('width', $value)) {
34-
yield (int) $value['width'];
35-
}
27+
yield (int) $value['width'];
3628
}
3729

3830
}

0 commit comments

Comments
 (0)