Jump to content

How to retrieve possible pages list in a Page field


tsdtsdtsd
 Share

Recommended Posts

Hi,

i have a field of the type Page. It has a node assigned (let's say node 'Test' with children a, b, c).

I want to retrieve the currently assigned set of pages (a,b,c) without doing a $pages search on the assigned node.

// What i don't want to do (to keep it dynamic)
$subset = $pages->get('/test')->children();

// What i want to do
$field = $fields->get('pagesField');
// ?
// $field->children() -> not defined
// $field->data->?

Any ideas? Thanks in advance :)

Link to comment
Share on other sites

It looks like in your example you simply want to get the value of all pages created using that Page field?

I thought they were always under the same parent/template unless you used a custom selector when setting up your field, so if this isn't what you want:

$pagelist = $pages->find('/path/to/parentnode/')->children();

then maybe this?

$pagelist = $pages->find('template=yourtemplate');

Other than that, if you used a custom selector when setting up your page field then you would simply use that selector again inside your template code.

Unless of course I've not understood what you're after ;)

Link to comment
Share on other sites

This happens because the page field is retrieving an array of pages, and not the page itself. You can solve it by choosing "single page" on the field setting, or by iterating through the result with a foreach(), or by choosing the first element on the array $page->pagefield->first()->children

Link to comment
Share on other sites

Thank you guys but you both got me wrong ;)

I'll try to clarify:

I created a custom field of the type Page.

I can assign a node to it (Test) and when using it in a template, it will give me the ability to choose one or more pages out of this set in the backend (childA, childB, childC). Let's say i selected childB.

I the frontend, i don't want to retrieve the selected page in this field (childB).

I want to get the set of pages that i could have selected or in other words, the set of pages which the backend would show me to choose from (childA, childB, childC).

I could retrieve those pages with $pages->get(), but i want to keep it dynamic. I don't want to change the code in the template if the parent node for the page-field is getting changed in backend.

So, with $fields->get() I can catch the field itself outside of page-context, but i dont know how to get the possible elements in this field. Or at least get the defined parent so i could use $pages->get() again.

Hope this clarifies :)

Link to comment
Share on other sites

Ah!!!

Ok, i did a print_r on $fields->pagefield->data, and got this:

Array
(
[derefAsPage] => 0
[parent_id] => 1044
[labelFieldName] => title
[inputfield] => InputfieldPageListSelectMultiple
[size] => 10
[required] => 1
)

So, you can get the parent id like this:

$fields->pagefield->data['parent_id']

And the children like this:

$parentId= $fields->pagefield->data['parent_id'];
$children= $pages->get($parentId)->children;

edit: this feels a bit hackish. I wonder if there's a simpler way to do this in PW...

  • Like 1
Link to comment
Share on other sites

I guess this does the same thing.

for multiple pages field:

$mySiblings = $page->yourpageselectfield->first()->siblings();

for single page field:

$mySiblings = $page->yourpageselectfield->siblings();
Link to comment
Share on other sites

Here's another option:


$field = $fields->get('your_page_select_field'); 
$inputfield = $field->type->getInputfield($page); 
$options = $inputfield->getSelectablePages($page); 

The advantage is that it'll work with all selectable pages settings (i.e. template, selectors, code, parent). Whereas the previous examples assume the selection is built just around a parent (which is perfectly fine too if that suits your needs).

  • Like 5
Link to comment
Share on other sites

  • 1 year later...

I love it when I stumble across an old piece of wisdom on a Saturday morning :)

Just wanted to note that there is a small mistake in Ryan's example. You need to remove "->type" from the second line, so full working example is:

$field = $fields->get('your_page_select_field');
$inputfield = $field->getInputfield($page);
$options = $inputfield->getSelectablePages($page);

https://github.com/ryancramerdesign/ProcessWire/blob/03387f8283d518e9cc405eff8f05cd6a5bf77c4c/wire/modules/Fieldtype/FieldtypePage.module#L308

Thanks again Ryan!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...