Jump to content

Get the current repeater page in Custom PHP code to find selectable pages


Ivan Gretsky
 Share

Recommended Posts

Good day!

I got a repeater with 2 Page fields inside of it. The second one is only visible if the 1st one has a value. This is possible since this release. Now I need the selectable pages of the second Page field to be the children of the 1st one. And this is where I am in trouble: the $page variable returns the page that the repeater is attached to, and $this return the InputField. Is there any way to get the current repeater page in the Custom PHP code to find selectable pages?

Link to comment
Share on other sites

2 hours ago, Ivan Gretsky said:

Is there any way to get the current repeater page in the Custom PHP code to find selectable pages?

I had a crack at this and it seems the answer is "yes". Here is example code you could use in the ""Custom PHP code to find selectable pages" for Page field 2:

$pid =  filter_var($this->name, FILTER_SANITIZE_NUMBER_INT); // get repeater page id from inputfield name
$p = $pages->get($pid); // get repeater page
if($p->id) {
	$parent = $p->page_field_1; // get value of first page field
	if($parent->id) {
		return $parent->children(); // return children of selected page
	}
}

But the bad news is that dependent Page fields only auto-refresh when using "Custom selector to find selectable pages" and not when using "Custom PHP code to find selectable pages". So you would need to save the page after selecting a value in Page field 1, which kinda sucks.

  • Like 1
Link to comment
Share on other sites

@Robin S
Your code will not work the naming format of a repeater page is based on microtime. You maybe talking about the parent. The format is 'for-page-1234'
Unfortunately FILTER_SANITIZE_NUMBER_INT will not remove the dashes (minus). To get the page where this repeater item lives you can go this way:

$mypage = $pages->get('id='.ltrim(strrchr($page->parent->name ,'-'),'-')); // the page where this repeater item lives

 

Edited by kixe
Correction
  • Like 2
Link to comment
Share on other sites

@kixe

The name of the repeater page doesn't come into the code I posted. This line...

$pid =  filter_var($this->name, FILTER_SANITIZE_NUMBER_INT); // get repeater page id from inputfield name

...gets the ID of the repeater page from the inputfield name - in the context of "Custom PHP code to find selectable pages " $this is the Page inputfield. The inputfield name includes the repeater page ID due to this: https://github.com/ryancramerdesign/ProcessWire/blob/devns/wire/modules/Fieldtype/FieldtypeRepeater/InputfieldRepeater.module#L219

But thinking some more, the base inputfield name itself could include integers so this would be better:

$pid =  filter_var(strrchr($this->name, "_"), FILTER_SANITIZE_NUMBER_INT); // get repeater page id from inputfield name

The whole idea is pretty hacky, but just trying to work around what is available in "Custom PHP code to find selectable pages".

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

today i came up with this solution. it finds the page related to a pagetable item, but it should also work for repeaters, i guess? for repeaters there is also the getForPage() method, wouldn't it be possible for you to work with this?

my code:

// find the related event page to the current coupon item
$current = $pages->findOne('template=event, couponcodes.id=' . $page->id);
// return all tickets of this event so that the user
// can choose for wich tickets the coupon is valid
return $current->tickets;

it finds the page of type "event" that has the current item ($page->id) as part of its pagetable called "couponcodes"

drawback is that this only works after saving. but thats necessary since the item wouldn't exist before saving...

maybe i'm getting you wrong. but maybe it helps someone :)

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