Skip to content

Commit 5049715

Browse files
authored
feat(type): add support for $page->references() api
* Trim whitespace * Add $page->references() to PageType
1 parent 1a05b31 commit 5049715

File tree

2 files changed

+52
-16
lines changed

2 files changed

+52
-16
lines changed

ProcessGraphQLConfig.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function getDefaults()
4545
'name',
4646
'httpUrl',
4747
'template',
48+
'references',
4849
],
4950

5051
/**
@@ -113,12 +114,12 @@ public function generatePages()
113114
$this->files->copy($from, $to);
114115
$this->message(sprintf($this->_('Created template files: %s'), 'graphql.php & graphiql.php'));
115116
} catch(\Exception $e) {
116-
$this->error($e->getMessage());
117+
$this->error($e->getMessage());
117118
}
118-
119+
119120
foreach($pageNames as $name => $title) {
120121

121-
// create the templates
122+
// create the templates
122123
try {
123124
$template = $this->wire(new Template());
124125
$template->name = $name;
@@ -131,10 +132,10 @@ public function generatePages()
131132
$template->noAppendTemplateFile = true;
132133
$template->noPrependTemplateFile = true;
133134
$template->save();
134-
$this->message(sprintf($this->_('Added template and fieldgroup: %s'), $name));
135+
$this->message(sprintf($this->_('Added template and fieldgroup: %s'), $name));
135136
} catch(\Exception $e) {
136-
$this->error($e->getMessage());
137-
continue;
137+
$this->error($e->getMessage());
138+
continue;
138139
}
139140

140141
// create the pages
@@ -145,10 +146,10 @@ public function generatePages()
145146
$p->parent = $this->pages->get(1); // root page
146147
$p->title = $title;
147148
$p->save();
148-
$this->message(sprintf($this->_('Created page: %s'), $name));
149+
$this->message(sprintf($this->_('Created page: %s'), $name));
149150
} catch(\Exception $e) {
150-
$this->error($e->getMessage());
151-
continue;
151+
$this->error($e->getMessage());
152+
continue;
152153
}
153154
}
154155
}

src/Type/PageType.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public static function getBuiltInFields()
5252
self::resolvePagefieldWithSelector([
5353
'name' => 'child',
5454
'type' => $type,
55-
'description' => "The first child of this page. If the `s`(selector) argument is provided then the
55+
'description' => "The first child of this page. If the `s`(selector) argument is provided then the
5656
first matching child (subpage) that matches the given selector. Returns a Page or null.",
5757
]),
5858

5959
self::resolvePagefieldWithSelector([
6060
'name' => 'children',
6161
'type' => PageArrayType::type(),
62-
'description' => "The number of children (subpages) this page has, optionally limiting to visible
63-
pages. When argument `visible` true, number includes only visible children
62+
'description' => "The number of children (subpages) this page has, optionally limiting to visible
63+
pages. When argument `visible` true, number includes only visible children
6464
(excludes unpublished, hidden, no-access, etc.)",
6565
]),
6666

@@ -109,8 +109,8 @@ public static function getBuiltInFields()
109109
[
110110
'name' => 'numChildren',
111111
'type' => Type::nonNull(Type::int()),
112-
'description' => "The number of children (subpages) this page has, optionally limiting to
113-
visible pages. When argument `visible` true, number includes only visible
112+
'description' => "The number of children (subpages) this page has, optionally limiting to
113+
visible pages. When argument `visible` true, number includes only visible
114114
children (excludes unpublished, hidden, no-access, etc.)",
115115
'args' => [
116116
'visible' => [
@@ -153,6 +153,12 @@ public static function getBuiltInFields()
153153
'description' => "The page's URL path from the homepage (i.e. /about/staff/ryan/)",
154154
],
155155

156+
self::resolveReferencesWithSelector([
157+
'name' => 'references',
158+
'type' => Type::nonNull(PageArrayType::type()),
159+
'description' => "Return this page's parent pages as PageArray. Optionally filtered by a selector and a field name.",
160+
]),
161+
156162
[
157163
'name' => 'template',
158164
'type' => Type::nonNull(Type::string()),
@@ -273,6 +279,35 @@ public static function resolvePagefieldWithSelector(array $options)
273279
]);
274280
}
275281

282+
public static function resolveReferencesWithSelector(array $options)
283+
{
284+
return array_merge($options, [
285+
'args' => [
286+
's' => [
287+
'type' => SelectorType::type(),
288+
'description' => "ProcessWire selector."
289+
],
290+
'f' => [
291+
'type' => Type::string(), // TODO: could also be of type Field or bool
292+
'description' => "ProcessWire field name."
293+
]
294+
],
295+
'resolve' => function (Page $page, array $args) use ($options) {
296+
$name = $options['name'];
297+
$selector = "";
298+
if (isset($args['s'])) {
299+
$selector = SelectorType::parseValue($args['s']);
300+
} else {
301+
$selector = SelectorType::parseValue("");
302+
}
303+
$field = isset($args['f']) ? $args['f'] : "";
304+
$result = $page->$name($selector, $field);
305+
if ($result instanceof NullPage) return null;
306+
return $result;
307+
}
308+
]);
309+
}
310+
276311
public static function getEmptyUser()
277312
{
278313
if (self::$emptyUser instanceof WireData) {
@@ -321,7 +356,7 @@ public static function resolveWithDateFormatter(array $options)
321356
],
322357
'resolve' => function (Page $page, array $args) use ($options) {
323358
$name = $options['name'];
324-
359+
325360
if (isset($args['format'])) {
326361
$format = $args['format'];
327362
$rawValue = $page->$name;
@@ -331,7 +366,7 @@ public static function resolveWithDateFormatter(array $options)
331366
return "";
332367
}
333368
}
334-
369+
335370
return $page->$name;
336371
}
337372
]);

0 commit comments

Comments
 (0)