Jump to content

Repeater: how to find the url of the parent page?


joe_ma
 Share

Recommended Posts

Hello

I am still struggling with repeaters and cannot find a solution for this one:

For a festival site I put up pages for the events (every page contains a single event). For the names of the performers I use a repeater field called "mitwirkend".

Now I should like to have a directory, that lists all the performers. Each name should link to the event, where it is listed.

My code looks like this:

    $programm = $pages->find("template=event"); //find all pages with events
    
    $mw_name = array();  // array for the names of the performers

    foreach ($programm as $p) { loop through the pages found
        
        $mitwirkende = $p->mitwirkend; //find the repeater fields
		        foreach ($mitwirkende as $mw) {  // loop through the repeater
					
					$mw_name[] = $mw->mw_name;
					$intlink = $mw->parent->url;  //get the url of the parent page
 
			} //end repeater loop
	} //end page loop
	
	asort($mw_name);
	
	foreach ($mw_name as $key => $m){
	
	echo "<p><a href='$intlink'>{$m}</a></p>";
		
	}

That lists all the names of the performers as expected.

But the links are not correct. Every href contains the same link, the one from the last event page it is looping through.

How could I associate the correct url to each name?

Thanks for help.

Link to comment
Share on other sites

Ah, … not quite.

I think the url part is not correlated to the items in the $mw_name array. But I have no clue about how to correlate them.

Can I ask why you're not considering using pages for the performers here? It would seem a perfect use case for them.

Because I find it easyer to list the performers directly on the event page. Like that, no one gets forgotten.

Also, making a list of all the performers first is more work to do … ;-)

Link to comment
Share on other sites

Ah, … not quite.

I think the url part is not correlated to the items in the $mw_name array. But I have no clue about how to correlate them.

Because I find it easyer to list the performers directly on the event page. Like that, no one gets forgotten.

Also, making a list of all the performers first is more work to do … ;-)

Joe, you still can list them directly on the event page and you don't ever have to go to a separate page to add new ones. You can create new performers directly from the event page. It would work as follows:

1) Create two templates - "performers" and "performer" (can have their own file or not)

2) Create a page on the pagetree called performers and give it the "performers" template. Now any children of this can be individual performers. 

3) Create a field of type "Page" call it "performer". On the input tab, set the selectable pages parent to be "Performers" and the template to be "performer" and at the bottom click the checkbox "allow new pages to be created from field". Now add this performer field to the event template.

You don't ever have to worry about performers again. Just add them as needed to the event page.

Note: You can set this field to allow 1 performer or multiple performers. This is generally the advisable way to perform this sort of relationship in PW (it is far easier to search, sort and work with the API when the performers have their own page)

  • Like 1
Link to comment
Share on other sites

Hi onjegolders

the checkbox "allow new pages to be created from field". Now add this performer field to the event template.

Ah, I haven't been aware, that there is also this possibility. I have always thought, you would have to create all the pages first, in order to use them on pages.

As always: PW is far easier than you would think.

Thanks onjegolders. You've made my day.

  • Like 1
Link to comment
Share on other sites

Hello

I am still struggling with repeaters and cannot find a solution for this one:

For a festival site I put up pages for the events (every page contains a single event). For the names of the performers I use a repeater field called "mitwirkend".

Now I should like to have a directory, that lists all the performers. Each name should link to the event, where it is listed.

My code looks like this:

    $programm = $pages->find("template=event"); //find all pages with events
    
    $mw_name = array();  // array for the names of the performers

    foreach ($programm as $p) { loop through the pages found
        
        $mitwirkende = $p->mitwirkend; //find the repeater fields
		        foreach ($mitwirkende as $mw) {  // loop through the repeater
					
					$mw_name[] = $mw->mw_name;
					$intlink = $mw->parent->url;  //get the url of the parent page
 
			} //end repeater loop
	} //end page loop
	
	asort($mw_name);
	
	foreach ($mw_name as $key => $m){
	
	echo "<p><a href='$intlink'>{$m}</a></p>";
		
	}

i think there are some unnecessary steps here, you could just do this:

$mw = $pages->find("template=repeater_mitwirkend, check_access=0");

http://cheatsheet.processwire.com/selectors/built-in-page-selector-properties/check_access-0/

then you have all the performers; to get the page you use:

$event = $mw->getForPage();

https://processwire.com/talk/topic/958-repeatable-fields/page-12

  • Like 1
Link to comment
Share on other sites

Many thanks Macrura. I have already rebuilt the site as suggested by onjegolders.

But I think your hint for getting the repeater fields helped me with another problem.

Though not quite yet. May I kindly ask you to have a look at that thread as well?

Link to comment
Share on other sites

sure, and you are better off anyway having the performers as pages as onjegolders suggested, but since repeaters are also a type of page, it's also good to know how to work with them as such, because sometimes you do need to build things that way, when for example a repeater object is going to be permanently attached to a page, there's no reason to have it be separate;

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