easynowbaby Posted April 22, 2019 Share Posted April 22, 2019 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 More sharing options...
Sergio Posted April 22, 2019 Share Posted April 22, 2019 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. 1 Link to comment Share on other sites More sharing options...
Robin S Posted April 22, 2019 Share Posted April 22, 2019 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? 2 Link to comment Share on other sites More sharing options...
Sergio Posted April 23, 2019 Share Posted April 23, 2019 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! ? 1 Link to comment Share on other sites More sharing options...
easynowbaby Posted April 26, 2019 Author Share Posted April 26, 2019 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 More sharing options...
Robin S Posted April 28, 2019 Share Posted April 28, 2019 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: Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now