Jump to content

repeater: one sub-field depending on another sub-field?


Recommended Posts

Hi there! And thanks for Processwire which i really like!

Got a specific repeater-related question which i couldn't resolve. Will appreciate any help.

Let's imagine we have Places (pages), Offers (pages) and Partners (PW users).

Any Place can have many Offers attached to it via a repeater field.

Any Partner can have many Places (repeater or a multiple-value field). He can offer some Offers at each Place he is attached to. His Offers for any Place should be selectable only from Offers already attached to this Place.

Partner-Places-Offers management should be performed in admin interface via PW input fields.

The ideal solution which i imagined could be a "dynamic" partner_place_offers repeater where multiple Offers for a single Place could be selected from dynamically built list dependent on selected Place. But it appears that PW doesn't have such functionality yet.

Thanks in advance for giving some idea on the subject!


Link to comment
Share on other sites

I think you can do this with core PW fields, and enhance it with some custom JS if you want. Here's what I'm thinking...

You have your Place pages in their own branch of the tree. The Place template has a repeater field "Offers" where all the possible offers for that Place are defined.

Then you have a repeater field "Places" on your Partner template. You add two Page Reference fields to the repeater:
1. "Place", which is a single select field limited to the Place template.
2. "Choose offers" which is a multiple AsmSelect field using a "Custom PHP code" hook.

The hook will return the value of the "Offers" repeater field (which will be a PageArray) for the place that is selected in the "Place" field. Now this hook is a bit trickier than normal because your Page Reference fields are inside a repeater and so you don't want $page to be the page being edited, but rather the repeater page that contains your fields. So in the hook you need to get the inputfield name and extract the ID integer portion of it and use that to get the $page object. I can explain more about this if you decide to go down this road.

So this approach should work but it will require editors to save the page after choosing a place in the "Place" select, before they can choose offers for that place.

If you wanted to enhance it a bit more you could have the "Choose offers" field allow all offers pages (limit using the Offers repeater template rather than a custom PHP hook) and then use some custom JS in admin to empty the inputfield options on load and on change of the "Place" select it gets the offers for that place via AJAX and populates the "Choose offers" options.

  • Like 2
Link to comment
Share on other sites

Thanks a lot Robin S! Will use almost all of Your solutions in process of "progressive development".

Step 1: a repeater with all available Offers for any Place, editors are responsible to enter the correct data.

Step 2: a hook which restricts Offers after saving the Place.

Step 3: Some custom JS.

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

  • Create New...