Skip to content

Commit 1d281ae

Browse files
author
Bertrand Dunogier
committed
Fixed error on field variation(s) for empty image fields (#66)
1 parent 21f1b25 commit 1d281ae

File tree

2 files changed

+36
-9
lines changed

2 files changed

+36
-9
lines changed

GraphQL/Resolver/ImageFieldResolver.php

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
namespace BD\EzPlatformGraphQLBundle\GraphQL\Resolver;
77

88
use eZ\Publish\API\Repository\ContentService;
9+
use eZ\Publish\Core\FieldType;
910
use eZ\Publish\SPI\Variation\VariationHandler;
1011
use Overblog\GraphQLBundle\Error\UserError;
1112
use eZ\Publish\Core\FieldType\Image\Value as ImageFieldValue;
@@ -25,29 +26,45 @@ class ImageFieldResolver
2526
* @var array
2627
*/
2728
private $variations;
29+
/**
30+
* @var FieldType\Image\Type
31+
*/
32+
private $fieldType;
2833

29-
public function __construct(VariationHandler $variationHandler, ContentService $contentService, array $variations)
34+
public function __construct(
35+
FieldType\Image\Type $imageFieldType,
36+
VariationHandler $variationHandler,
37+
ContentService $contentService,
38+
array $variations
39+
)
3040
{
3141
$this->variationHandler = $variationHandler;
3242
$this->contentService = $contentService;
3343
$this->variations = $variations;
44+
$this->fieldType = $imageFieldType;
3445
}
3546

3647
public function resolveImageVariations(ImageFieldValue $fieldValue, $args)
3748
{
49+
if ($this->fieldType->isEmptyValue($fieldValue)) {
50+
return null;
51+
}
3852
list($content, $field) = $this->getImageField($fieldValue);
3953

4054
$variations = [];
4155
foreach ($args['identifier'] as $identifier) {
42-
$versionInfo = $this->contentService->loadVersionInfo($content->contentInfo);
43-
$variations[] = $this->variationHandler->getVariation($field, $versionInfo, $identifier);
56+
$variations[] = $this->variationHandler->getVariation($field, $content->versionInfo, $identifier);
4457
}
4558

4659
return $variations;
4760
}
4861

4962
public function resolveImageVariation(ImageFieldValue $fieldValue, $args)
5063
{
64+
if ($this->fieldType->isEmptyValue($fieldValue)) {
65+
return null;
66+
}
67+
5168
list($content, $field) = $this->getImageField($fieldValue);
5269
$versionInfo = $this->contentService->loadVersionInfo($content->contentInfo);
5370

@@ -56,17 +73,13 @@ public function resolveImageVariation(ImageFieldValue $fieldValue, $args)
5673

5774
/**
5875
* @param ImageFieldValue $fieldValue
59-
* @return array
76+
* @return [Content, Field]
6077
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException
6178
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException
6279
*/
6380
protected function getImageField(ImageFieldValue $fieldValue): array
6481
{
65-
$idArray = explode('-', $fieldValue->imageId);
66-
if (count($idArray) != 3) {
67-
throw new UserError("Invalid image ID {$fieldValue->imageId}");
68-
}
69-
list($contentId, $fieldId, $versionNumber) = $idArray;
82+
list($contentId, $fieldId, $versionNumber) = $this->decomposeImageId($fieldValue);
7083

7184
$content = $this->contentService->loadContent($contentId, [], $versionNumber);
7285

@@ -90,4 +103,17 @@ protected function getImageField(ImageFieldValue $fieldValue): array
90103

91104
return array($content, $field);
92105
}
106+
107+
/**
108+
* @param ImageFieldValue $fieldValue
109+
* @return array
110+
*/
111+
protected function decomposeImageId(ImageFieldValue $fieldValue): array
112+
{
113+
$idArray = explode('-', $fieldValue->imageId);
114+
if (count($idArray) != 3) {
115+
throw new UserError("Invalid image ID {$fieldValue->imageId}");
116+
}
117+
return $idArray;
118+
}
93119
}

Resources/config/resolvers.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ services:
106106

107107
BD\EzPlatformGraphQLBundle\GraphQL\Resolver\ImageFieldResolver:
108108
arguments:
109+
$imageFieldType: "@ezpublish.fieldType.ezimage"
109110
$variationHandler: "@ezpublish.fieldType.ezimage.variation_service"
110111
$contentService: "@ezpublish.siteaccessaware.service.content"
111112
$variations: "$image_variations$"

0 commit comments

Comments
 (0)