Jump to content

Populate Select from another Field/Template


Bike
 Share

Recommended Posts

Hi everyone,

on some preferences template there is a repeater field containing fields like 'link' and 'url' (and maybe some more). That repeater field is populated by the client.

On another template I'd like to have a select field which outputs the repeater's content each as an option so I can have it on the page like $select->link, $select->url, $select->whatever_is_in_the_repeater_field

I think this should acutally be a common thing to do but I don't know how to accomplish it.

Any ideas? Thanks ?

Link to comment
Share on other sites

If I'm reading this right, you can do this by creating a page reference and specifying "template=repeater_yourfield".

You should be fine in terms of access, but you will want to test using their role if they aren't superusers.

If the select box is blank for the user you can use a custom find for the page reference and add 'include=all' or check permissions. 

Link to comment
Share on other sites

Thanks @outward,

what I actually need (in the admin) is a repeater with a various amount of fields and this repeater to be connected to a select field.

I am not quite sure if I can achieve this with your suggestion. Maybe you or someone else has another thread on hand?!

Thanks!

Link to comment
Share on other sites

@Bike already put you on the right track.

I have an example setup that does just this: pull values from a repeater field and display them in a select dropdown.

Here's how I made it work.

1. have a repeater field called 'repeater' on a template. The repeater field can have as many fields on it as you like. Mine only has one field named 'headline'
2. make some entries to that field
477845676_2021-04-01-135057.thumb.png.0ee20abaf4ea30d7b4e53f1a62a01016.png

3. setup a page reference field, mine is named 'selectfromrepeater'. It has Single Page selected in Details tab. In the Input tab, I have following settings
1198131895_2021-04-01-142303.thumb.png.ee054ae11b59275e47eec8544131366f.png
 
See the Label field? Here I have entered the field 'headline' from my repeater field. In your case you can enter 'link' or 'url'
5. I'm using the Custom PHP Code option to retrieve the selectable pages, so in my site/ready.php file I placed this hook:

$wire->addHookAfter('InputfieldPage::getSelectablePages', function ($event) {
    if ($event->object->hasField == 'selectfromrepeater') {
        $event->return = $event->pages->find('template=repeater_repeater');
    }
});

Now when I add the new field 'selectfromrepeater' to a template and edit a page with that template, I get something like this:
selectfromrepeater.png.43afa6aee9903179e96bd97d019bd061.png

If you wanted to retrieve the selection via API in a template file, you would do

$page->selectfromrepeater->headline // or ->url in your case

 

Hope that helps
 

Link to comment
Share on other sites

Quote

$wire->addHookAfter('InputfieldPage::getSelectablePages', function ($event) {
    if ($event->object->hasField == 'selectfromrepeater') {
        $event->return = $event->pages->find('template=repeater_repeater'); // Why 'template=repeater_repeater' ?
    }
});

@gebeer Where does 'template=repeater_repeater' comes from?

Link to comment
Share on other sites

5 hours ago, 3fingers said:

Where does 'template=repeater_repeater' comes from?

My repeater field's name is 'repeater'. So the template name of pages that this repeater creates in the background is 'repeater_repeater'. If my repeater field was named 'myrepeaterfield' then the template would be 'repeater_myrepeaterfield'.

Repeater fields create one page for each repeater entry to store the data. Those pages are located in the page tree under Admin->Repeaters. In my case it looks like
19748666_2021-04-01-211831.png.00199e91b576d752996d2c4f2ea38d79.png

If you edit one of those pages, you can see the template name that PW creates automatically
3778909_2021-04-01-211959.png.a6447a8995a44d3996b8f8dd88a591be.png

 

  • Thanks 1
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...