Jump to content

MultiplePageField I want to be able to change the selectable pages based on PHP


PeterDK
 Share

Recommended Posts

In a MultiplePageField I want to be able to change the selectable pages based on PHP.

This code

echo $pages->find("template=klassement,jaar=2013")->implode("|",toerist);

gets me to echo out all the id's, but this is a string, (using explode I  get an Array).

But PW needs to get a PWArray!

How to convert this list of Array into a PWArray? (for use in "Custom PHP code to find selectable pages")

Link to comment
Share on other sites

Peter,
 
Why do you need to implode anything? $pages->find already returns a PW PageArray. That's what that field requires. I am assuming you are talking about the Input tab in your MultiplePage Field....You also need to use return and not echo...There's an example right there below that field  :)

return $pages->find("template=klassement, jaar=2013");

If you want to find selectable pages using a PHP code snippet rather than selecting a parent page or template (above) then enter the code to find the selectable pages. This statement has access to the $page and $pages API variables, where $page refers to the page being edited. The snippet should return either a PageArray or NULL. Using this is optional, and if used, it overrides the parent/template/selector fields above. NOTE: Not compatible with PageListSelect or Autocomplete input field types.

Example: return $page->parent->parent->children("name=locations")->first()->children();

 Refresher: http://processwire.com/api/arrays/

Edited by kongondo
Link to comment
Share on other sites

Thanks for your quick reply, but

return $pages->find("template=klassement, jaar=2013");

isn't doing it for me (even if using the 'return' instead of echo -> that was one error).

That returns a list of pages, but I need from all those pages a field called 'toerist'. Wich is a page field.

So if I implode or explode, I get the list, wich is good, but I need it to be a PWArray.

Your thoughts?

Link to comment
Share on other sites

That's what a Page Field is for. It returns pages :-) Visually, you see the list of pages, but it actually returns the page objects. With those, in the front end, you have access to all fields and everything else about those pages. I don't get what you mean  by "isn't doing it for me" - don't you get the list of pages in your MultiPage field? If not, are you using a compatible Page List? But, it also sounds to me as if you want to display a page field (toerist) within this other page field where you have your custom PHP. Is that the case? That is not possible according to what I know :-)....but, assuming I am not getting you...and all you want is access to the page field (toerist) that you then want to display in the front end..., that is easy...

Let's call this PageField with your custom PHP "klassfield".....In it, you select the pages returned by your custom PHP...then, in your template file it is just a matter of looping through first, the $page->klassfield since it is an array. But, within the pages in that array (the ones that have the field toerist), the field toerist is also an array (assuming it is a MultiplePageField too)...so you will want to loop through those too. Does this make sense? If not, we could provide some example code :-)

Link to comment
Share on other sites

Yes, it is effectively a nested array.

Call the Page

$page

Call the page field within that page

$page->my_page_field

Call the field within the pages inside the page field

$page->my_page_field->some_field

So, loop trough the first to get the pages

foreach($page->my_page_field as $apage){

   echo $apage->some_field;
}

er ...  I think. Sorry, i am meant to be composing a track for a client ... I will run off.

Link to comment
Share on other sites

Peter....what do you mean you want to return it as a PW Array? It is already a PW array! What you do with it is up to you. You can echo it or store it in a variable. Maybe if you explained your use case better....(or I am just thick and don't get..which is often the case :P )

Link to comment
Share on other sites

Peter, just had some more thought about this...are you saying you want the value of toerist to be the selectable entity within the PageField with the custom PHP? i.e., you want to use toerist in the Admin and not the frontend...is that it? That wouldn't work as far as I can tell...this is a Page Field...I don't know of any method to display another pages field in any PW field...

Edited by kongondo
Link to comment
Share on other sites

Each page 'klassement' has one toerist field (wich is a page field). It's indeed the toerists that I'm after.

I want the people not to select the 'klassement', but the related toerist.

Sorry that I don't give you a clear question...

Link to comment
Share on other sites

Let's go back to the first post. The answer is almost there, we just need to do a small modification:

$peterIDs = $pages->find("template=klassement,jaar=2013")->implode("|","toerist");

return $pages->find("id={$peterIDs}");

voilá :)

  • Like 8
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...