@@ -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