|
7 | 7 | use Youshido\GraphQL\Type\Scalar\IntType; |
8 | 8 | use ProcessWire\GraphQL\Type\Object\PageImageType; |
9 | 9 | use Youshido\GraphQL\Execution\ResolveInfo; |
| 10 | +use ProcessWire\GraphQL\Utils; |
| 11 | +use ProcessWire\GraphQL\Field\PageImage\EmptyPageImage; |
10 | 12 |
|
11 | 13 | class PageImageSizeField extends AbstractField{ |
12 | 14 |
|
@@ -39,8 +41,26 @@ public function build(FieldConfig $config) |
39 | 41 |
|
40 | 42 | public function resolve($value, array $args, ResolveInfo $info) |
41 | 43 | { |
| 44 | + $canCreate = Utils::moduleConfig()->legalEditFields->has($value->field); |
42 | 45 | $width = isset($args['width']) ? $args['width'] : null; |
43 | 46 | $height = isset($args['height']) ? $args['height'] : null; |
44 | | - return $value->size($width, $height); |
| 47 | + |
| 48 | + // if there neither width nor heigth is given then we return empty image |
| 49 | + if (!$width && !$height) return new EmptyPageImage(); |
| 50 | + |
| 51 | + // we create the image if user have rights for it |
| 52 | + if ($canCreate) return $value->size($width, $height); |
| 53 | + |
| 54 | + // if user has no rights to create the image then she |
| 55 | + // might be asking for variation already created |
| 56 | + $options = []; |
| 57 | + if ($width) $options['width'] = $width; |
| 58 | + if ($height) $options['height'] = $height; |
| 59 | + $thumbs = $value->getVariations($options); |
| 60 | + |
| 61 | + // If we find the variation then return it |
| 62 | + // otherwise return empty |
| 63 | + if ($thumbs->count()) return $thumbs->first(); |
| 64 | + return new EmptyPageImage(); |
45 | 65 | } |
46 | 66 | } |
0 commit comments