Jump to content

Getting page field value in repeater inside hook


Kiwi Chris
 Share

Recommended Posts

I have a hook inside ready.php and I'm trying to get one page reference field pageBatch inside the repeater to update based on the value of another page reference field also inside the repeater, but so far haven't been able to get it to work.

Is this possible, or is it a limitation of repeaters? 

Ideally I'd prefer to use a repeater than pagetable, as there will normally only ever be two or three items in the repeater, and the data entry with repeaters is a bit more intuitive for users as they don't have to remember to publish, and the entry is inline, rather than pop-up modals.

$wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {
// pageBatch is a page reference field in a repeater
if ($event->object->hasField == 'pageBatch') {
        $page = $event->arguments('page'); //returns the page that the repeater field is in, not the current repeater item
		$repeater = $page->repeaterField; 
		//Want to be able to get the value of $repeater->pageField (a page reference field in the repeater)
		//Can refer to a page reference field in the page the repeater is in ok though eg $page->pageField works
		//but not a page reference field inside the repeater $repeater->pageField
    }
});

 

Link to comment
Share on other sites

  • 2 weeks later...

getForPage() doesn't help.

    if ($event->object->hasField == 'pageBatch') {
		bd($event->arguments(0), "Trigger Object");
        $page = $event->arguments('page');
        if ($page instanceof RepeaterPage) {
            $page = $page->getForPage();
            bd($page->dispatchItems, "DispatchItems");
        }

Here is the segment of of code. 

the first bd() indicates that $event->arguments(0) is returning the page the repeater field is used on, NOT the page of the current repeater item, even though pageBatch is a field within the repeater.

bd() doesn't even get called at the second instance, so even though the field pageBatch is contained within the RepeaterPage, I can't work out how to refer to any of the fields within the repeater.

Link to comment
Share on other sites

@Kiwi Chris, I think you want to make use of Inputfield::hasPage in this case.

$wire->addHookAfter('InputfieldPage::getSelectablePages', function(HookEvent $event) {
	/* @var InputfieldPage $inputfield */
	$inputfield = $event->object;
	$field = $inputfield->hasField;
	$edited_page = $event->arguments('page');
	$page_containing_inputfield = $inputfield->hasPage; // a Repeater page in your case
	if($field->name === 'pageBatch') {
		// Use $page_containing_inputfield as needed here
		// ...
	}
});

 

  • Like 1
Link to comment
Share on other sites

@Robin SThanks that's what I was looking for. hasPage returns the repeater page.

I think I now have the capability to deal with most scenarios for page fields where they have dependencies.

I think they still rely on the page being saved, but that's OK for now, although it would be great if I could get them updating via ajax.

Inside my hook I now have something like this:

    if ($event->object->hasField == 'pageBatch') {
        $inputfield = $event->object;
        $repeater = $inputfield->hasPage;
		//Now possible to refer to repeater fields in hook.
	}

 

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