Jump to content

Hook current page got created


rjgamer
 Share

Recommended Posts

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

Link to comment
Share on other sites

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

}

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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) { ... }

 

  • Like 1
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

×
×
  • Create New...