Alpine418 Posted March 13, 2021 Posted March 13, 2021 Hi, is there a hook after the current (active) page got created? Or which method got called in the Page class after the Constructor of the current page got initialized? Thanks.
Alpine418 Posted March 13, 2021 Author Posted March 13, 2021 3 hours ago, monollonom said: Have you tried with Pages::added ? Sorry, I'm mean after a page load when the current page get fetched from the database. Page::added get called when I create a new page. I've a use case where I want a run a method of my custom page class. But if I run the method in the constructor, not all properties of page are loaded (e.g. httpUrl).
Alpine418 Posted March 13, 2021 Author Posted March 13, 2021 I think I found it: Page::loaded https://processwire.com/api/ref/page/loaded/ Edit: How do I check in a custom page class, if it is the current class? Maybe something like this: namespace ProcessWire; class DefaultPage extends Page { public function loaded(): { parent::loaded(); if ($this->isCurrent) { // to my special custom super feature stuff } } }
monollonom Posted March 13, 2021 Posted March 13, 2021 I've never worked with custom page classes but I assume you could add a hook on the "loaded" event of your custom page and execute your custom function, something like : $wire->addHookAfter("CustomPage::loaded", function($event) { $page = $event->object; $page->yourCustomPageFunction(); });
bernhard Posted March 14, 2021 Posted March 14, 2021 20 hours ago, rjgamer said: namespace ProcessWire; class DefaultPage extends Page { public function loaded(): { parent::loaded(); if ($this->isCurrent) { // to my special custom super feature stuff } } } Are you sure you need to check? If you place a custom "loaded" method in your custom page class only pages using your custom page class will execute that code anyway... Or am I missing anything? But if I'm missing anything checking for the current pageclass is as easy as this: if($page instanceof self) { ... } 1
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