Jump to content

Cannot access page reference fileds in a hook


Recommended Posts

Hello everyone,

I have a hook in my module that's hooking after a page was created. Inside of it I need to access the data of the newly created page and send the data in an email. My problem is that whenever I try to access a page reference type of field, it seems that it's empty. Here's the hook: 

 

public function ready() {
        $this->pages->addHookAfter('added', function ($event) {
            $p = $event->arguments[0];

            if ($p->template == "order") {
                // accessing page reference fields here doesn't work
            }
        });
    }

Any help would be greatly appreciated!

Link to comment
Share on other sites

public function ready() {
    $this->pages->addHookAfter('added', function ($event) {
        $p = $event->arguments[0];

        if ($p->template->name === "order") {
            // accessing page reference fields here doesn't work
        }
    });
}

You forgot to check for the template name.

  • Like 1
Link to comment
Share on other sites

8 hours ago, Sergio said:

You forgot to check for the template name

Testing against the template object with the == "equal" operator is okay because it means "equal to after type juggling". So the template object is converted to a string for the comparison, and thanks to Template::__toString() the the string value of a template object is its name.

11 hours ago, easynowbaby said:

I have a hook in my module that's hooking after a page was created

When a page is first created (via admin) the only populated fields are Title and Name. So Page Reference fields are no different to any other field - they are empty until they are populated and saved in Page Edit. Maybe you want to hook Pages::published instead?

  • Like 2
Link to comment
Share on other sites

2 hours ago, Robin S said:

Testing against the template object with the == "equal" operator is okay because it means "equal to after type juggling". So the template object is converted to a string for the comparison, and thanks to Template::__toString() the the string value of a template object is its name.

Ops! Didn't know that, thank you Robin! ?

  • Like 1
Link to comment
Share on other sites

On 4/23/2019 at 12:41 AM, Robin S said:

When a page is first created (via admin) the only populated fields are Title and Name. So Page Reference fields are no different to any other field - they are empty until they are populated and saved in Page Edit. Maybe you want to hook Pages::published instead?

Thank you for your reply, but my problem lies elsewhere. The page is not created via admin, I use PW as a backend for a SPA, so it is being created via api and I'm trying to access fields that are not empty. Basically I'm trying to do something like this:

$p->order_status->first->title->getLanguageValue(1027)

This works in a different hook (ProcessPageEdit::processInput) but not in published or added. Any ideas appreciated...

Link to comment
Share on other sites

Getting Page Reference field values in a Pages::added() hook is working here.

The hook:

$pages->addHookAfter('added', function(HookEvent $event) {
	$page = $event->arguments(0);
	if($page->template == 'colour') {
		bd($page->countries->explode('title'), $page->title);
	}
});

Page add code and dump result:

2019-04-28_191539.png.bcfa2b15ad807a671423c0b4dc50f146.png

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