Jump to content

Executing some code when certain configurable module is saved


titanium
 Share

Recommended Posts

Hi, I would like to build a module which executes some functions when it's config dialog is saved. That's what I have so far:

<?php

class Patch extends WireData implements Module
{
    public function init()
    {
        $this->addHookAfter('Modules::saveConfig', $this, 'runPatch');
    }

    public function runPatch(HookEvent $event)
    {
        $this->message('runPatch has been executed');
    }
}

This works so far, but it works for any module which is saved, which is not what I want - I just want to run the method "runPatch" when this particular module has been saved. How can I achieve that?

Link to comment
Share on other sites

It's all about checking the $event.

Using Tracy, inside your runPatch() method, do this:

bd($event);

That will return something like:

Screen Shot 2016-11-11 at 11.02.45 AM.png

Now that shows that you are looking for: $event->arguments[0]

Just to confirm, do:

bd($event->arguments[0]);

and you'll get:

Screen Shot 2016-11-11 at 11.04.33 AM.png

which shows that you can simply do:

if($event->arguments[0] === 'MyModuleName') {

 

  • Like 5
Link to comment
Share on other sites

Aaah, yes - I see clearly now. That works - many thanks for this :-)

Since I'm often to confused to remember that I have used the module's name more than just in the filename: is there a smart way to use the name of the module somewhat more dynamically, besides parsing the module's filename?

Link to comment
Share on other sites

3 minutes ago, titanium said:

Since I'm often to confused to remember that I have used the module's name more than just in the filename: is there a smart way to use the name of the module somewhat more dynamically, besides parsing the module's filename?

Sorry I am not understanding what you are asking here. In the example above we are not parsing the filename. Perhaps you can provide some code showing what you are currently doing.

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