Jump to content

Hook is also triggered when a page is saved through the API


jrtderonde
 Share

Recommended Posts

Hey,

I made a hook that is triggered when a page with a certain template is saved. I meant to create the script only for the backend of the website.

Now, whenever a page is saved on the front end (with the API) the module is being triggered. I was wondering if there is a method to have it only trigger whenever a page is saved on the backend (so not through the API).

 

Jim

Link to comment
Share on other sites

In most cases you can simply do:

if($page->template == "admin") {

but this needs to be in ready(). If you need it to work within init(), then I use:

if(strpos($_SERVER['REQUEST_URI'], wire('config')->urls->admin) === 0) {

 

  • Like 3
Link to comment
Share on other sites

Alternatively you can have your module autoload only in the PW admin. Then the hooks you add in init() won't apply to API code you use in a template file.

public static function getModuleInfo() {
    return array(
        'title' => 'My Module',
        'version' => '1',
        'summary' => "Module summary.",
        'autoload' => "template=admin",
    );
}

 

  • Like 3
Link to comment
Share on other sites

Just now, Robin S said:

Alternatively you can have your module autoload only in the PW admin. Then the hooks you add in init() won't apply to API code you use in a template file.


public static function getModuleInfo() {
    return array(
        'title' => 'My Module',
        'version' => '1',
        'summary' => "Module summary.",
        'autoload' => "template=admin",
    );
}

 

This is a great idea (better than mine) if nothing in the module needs to work on the frontend, but one thing to note - if you change the autoload from true to template=admin you will need to do a Modules > Refresh to have the change take effect - I remember pulling some hairs in my early days of module development wondering why it wasn't working :)

  • Like 4
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...