Jump to content
Kiwi Chris

Getting page field value in repeater inside hook

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
    }
});

 

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
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

Share this post


Link to post
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.
	}

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...