Skip to content

Commit b55daa0

Browse files
committed
Fix FieldtypePage for empty value and derefAsPageOrFalse setting.
1 parent cdd637c commit b55daa0

File tree

3 files changed

+71
-8
lines changed

3 files changed

+71
-8
lines changed

src/Type/Fieldtype/FieldtypePage.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use GraphQL\Deferred;
44
use ProcessWire\Page;
5+
use ProcessWire\PageArray;
56
use ProcessWire\FieldtypePage as PWFieldtypePage;
67
use ProcessWire\GraphQL\Cache;
78
use ProcessWire\GraphQL\Type\Inputfield\InputfieldPage;
@@ -37,17 +38,24 @@ public static function field($field)
3738
'description' => $desc,
3839
'type' => self::type($field),
3940
'resolve' => function ($value, $args, $context, $info) use ($field) {
41+
$field->derefAsPage = PWFieldtypePage::derefAsPageArray;
4042
$data = $value->getArray();
4143
if (isset($data[$field->name])) {
42-
PagesBuffer::add($field->name, $data[$field->name]);
44+
$data = $data[$field->name];
45+
if (!empty($data)) {
46+
PagesBuffer::add($field->name, $data);
47+
}
4348
}
4449
return new Deferred(function () use ($value, $field, $info) {
4550
$finderOptions = PageArrayType::getFinderOptions($info);
4651
PagesBuffer::loadPages($field->name, $finderOptions);
4752
$fieldName = $field->name;
4853
$field = \ProcessWire\wire('fields')->get($fieldName);
49-
$field->derefAsPage = PWFieldtypePage::derefAsPageArray;
50-
return $value->$fieldName->find(SelectorType::parseValue(""));
54+
$value = $value->$fieldName;
55+
if (!$value instanceof PageArray) {
56+
return new PageArray();
57+
}
58+
return $value->find(SelectorType::parseValue(""));
5159
});
5260
}
5361
];
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace ProcessWire\GraphQL\Test\Field\Page\Fieldtype;
4+
5+
/**
6+
* Test the case when FieldtypePage:derefAsPageOrFalse is set
7+
* and the value is empty.
8+
*/
9+
10+
use ProcessWire\FieldtypePage;
11+
use \ProcessWire\GraphQL\Test\GraphQLTestCase;
12+
use \ProcessWire\GraphQL\Utils;
13+
14+
class FieldtypePageCaseThreeTest extends GraphQLTestCase {
15+
16+
const settings = [
17+
'login' => 'admin',
18+
'legalTemplates' => ['skyscraper', 'architect'],
19+
'legalFields' => ['architects'],
20+
'access' => [
21+
'fields' => [
22+
[
23+
'name' => 'architects',
24+
'derefAsPage' => FieldtypePage::derefAsPageOrFalse,
25+
]
26+
]
27+
]
28+
];
29+
30+
public function testValue()
31+
{
32+
$skyscraper = Utils::pages()->get("template=skyscraper, architects.count=0");
33+
34+
// wake up the architects value
35+
// which will set it to false
36+
$skyscraper->architects;
37+
38+
$query = "{
39+
skyscraper (s: \"id=$skyscraper->id\") {
40+
list {
41+
architects {
42+
list {
43+
id
44+
name
45+
}
46+
}
47+
}
48+
}
49+
}";
50+
$res = self::execute($query);
51+
assertEquals(
52+
0,
53+
count($res->data->skyscraper->list[0]->architects->list),
54+
'Returns empty architect page array.'
55+
);
56+
57+
assertObjectNotHasAttribute('errors', $res, 'There are errors.');
58+
}
59+
60+
}

test/Field/Page/Fieldtype/FieldtypePage/FieldtypePageCaseTwoTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
*/
99

1010
use \ProcessWire\GraphQL\Test\GraphQLTestCase;
11-
use \ProcessWire\GraphQL\Test\Field\Page\Fieldtype\Traits\FieldtypeTestTrait;
1211
use \ProcessWire\GraphQL\Utils;
1312

1413
class FieldtypePageCaseTwoTest extends GraphQLTestCase {
@@ -18,10 +17,6 @@ class FieldtypePageCaseTwoTest extends GraphQLTestCase {
1817
'legalTemplates' => ['skyscraper', 'architect'],
1918
'legalFields' => ['architects'],
2019
];
21-
const FIELD_NAME = 'architects';
22-
const FIELD_TYPE = 'FieldtypePage';
23-
24-
use FieldtypeTestTrait;
2520

2621
public function testValue()
2722
{

0 commit comments

Comments
 (0)