Jump to content

Hook for when a module's configuration is updated


Marc
 Share

Recommended Posts

I want to build a simple module that holds some settings for my website. The easiest way I see is doing this with a WireData module. So I set one up and it currently holds one field called website_type. When this field is saved, I want to do some stuff with the updated value. How do I hook into the save method? I tried the following, showing a message with the updated value (it shows the current value instead):

class WireDataSetupWebsite extends WireData implements Module
{
    public static function getModuleInfo()
    {
        return array(
            "title" => "Website Options",
            "version" => "0.0.1",
            "summary" => "Setup options for this website.",
            "author" => "",
            "singular" => true,
            "autoload" => true
        );
    }

    public function init()
    {
        $this->addHookBefore('ProcessModule::executeEdit', $this, 'test');

    }
    
    public function test(HookEvent $event) {
        $this->session->message('website type: ' . $this->website_type);
    }

}

 

Link to comment
Share on other sites

7 hours ago, Marc said:

I want to build a simple module that holds some settings for my website. The easiest way I see is doing this with a WireData module.

I've always found that the easiest way to store site settings is with a dedicated template/page.

7 hours ago, Marc said:

How do I hook into the save method?

I'm a bit confused by your code because it looks like the module doesn't have any config fields, but generally speaking you can get your module config data after it is saved by hooking after Modules::saveConfig (or Modules::saveModuleConfigData if you need to support older versions of PW). You hook will run after the config is saved for any module, but you can use the first argument to compare against your module classname and return early if it doesn't match.

  • Like 3
Link to comment
Share on other sites

15 hours ago, Robin S said:

I'm a bit confused by your code because it looks like the module doesn't have any config fields, but generally speaking you can get your module config data after it is saved by hooking after Modules::saveConfig (or Modules::saveModuleConfigData if you need to support older versions of PW). You hook will run after the config is saved for any module, but you can use the first argument to compare against your module classname and return early if it doesn't match.

I did not include the config part of the module because I did not think it would be relevant. But the Modules::saveConfig hook looks like the hook I need, thanks. I tried it out with a simple test but this does nothing when I save the module configuration (it should diplay a message):

class WireDataSetupWebsite extends WireData implements Module
{
    public static function getModuleInfo()
    {
        return array(
            "title" => "Website Options",
            "summary" => "Setup options for this website.",
            "author" => "",
            "singular" => true,
            "autoload" => true
        );
    }

    public function init()
    {
        $this->addHookAfter('Module::saveConfig', $this, 'test');
    }
    
    public function test(HookEvent $event) {
        $arguments = $event->arguments;
        $this->session->message('this is a test to see if the hook runs');
    }

}

 

Link to comment
Share on other sites

  • 1 year later...
On 7/26/2018 at 11:50 PM, Robin S said:

but you can use the first argument to compare against your module classname and return early if it doesn't match.

Something like this?

$classname = $event->arguments[0];
$thisClassname = $this->wire('modules')->getModuleClass($this);
if ($classname !== $thisClassname) return; // Only continue if settings of this module are being saved.

 

Link to comment
Share on other sites

3 hours ago, szabesz said:

Something like this?


$classname = $event->arguments[0];
$thisClassname = $this->wire('modules')->getModuleClass($this);
if ($classname !== $thisClassname) return; // Only continue if settings of this module are being saved.

You can simplify that a bit to this...

$classname = $event->arguments[0];
if($classname !== $this->className) return; // Only continue if settings of this module are being saved.

 

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...