Skip to content

Commit ae9f396

Browse files
committed
Implement fallback for size field of PageImageType.
1 parent 911a9a3 commit ae9f396

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Field\PageImage;
4+
5+
use ProcessWire\WireData;
6+
use ProcessWire\WireArray;
7+
8+
class EmptyPageImage extends WireData {
9+
10+
public function getVariations()
11+
{
12+
return new WireArray();
13+
}
14+
15+
}

src/Field/PageImage/PageImageSizeField.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Youshido\GraphQL\Type\Scalar\IntType;
88
use ProcessWire\GraphQL\Type\Object\PageImageType;
99
use Youshido\GraphQL\Execution\ResolveInfo;
10+
use ProcessWire\GraphQL\Utils;
11+
use ProcessWire\GraphQL\Field\PageImage\EmptyPageImage;
1012

1113
class PageImageSizeField extends AbstractField{
1214

@@ -39,8 +41,26 @@ public function build(FieldConfig $config)
3941

4042
public function resolve($value, array $args, ResolveInfo $info)
4143
{
44+
$canCreate = Utils::moduleConfig()->legalEditFields->has($value->field);
4245
$width = isset($args['width']) ? $args['width'] : null;
4346
$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();
4565
}
4666
}

src/Type/InterfaceType/PageFileInterfaceType.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static function getPageFileFields()
5353
'type' => new StringType(),
5454
'description' => 'The web accessible URL (with scheme and hostname) to this Pagefile.',
5555
'resolve' => function ($value) {
56-
return (string) $value->httpUrl();
56+
return (string) $value->httpUrl;
5757
}
5858
],
5959

@@ -133,4 +133,4 @@ public static function getPageFileFields()
133133
}
134134

135135

136-
}
136+
}

0 commit comments

Comments
 (0)