Marc Posted July 26, 2018 Share Posted July 26, 2018 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 More sharing options...
adrian Posted July 26, 2018 Share Posted July 26, 2018 16 minutes ago, Marc said: I want to build a simple module that holds some settings for my website. Maybe just use: http://modules.processwire.com/modules/settings-factory/ 2 Link to comment Share on other sites More sharing options...
Robin S Posted July 26, 2018 Share Posted July 26, 2018 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. 3 Link to comment Share on other sites More sharing options...
Marc Posted July 27, 2018 Author Share Posted July 27, 2018 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 More sharing options...
Robin S Posted July 27, 2018 Share Posted July 27, 2018 7 hours ago, Marc said: I tried it out with a simple test but this does nothing when I save the module configuration You are hooking Module::saveConfig instead of Modules::saveConfig. 2 Link to comment Share on other sites More sharing options...
Marc Posted July 30, 2018 Author Share Posted July 30, 2018 On 7/27/2018 at 11:33 PM, Robin S said: You are hooking Module::saveConfig instead of Modules::saveConfig. Oops, that fixed it. Thank you very much! Link to comment Share on other sites More sharing options...
szabesz Posted September 1, 2019 Share Posted September 1, 2019 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 More sharing options...
Robin S Posted September 1, 2019 Share Posted September 1, 2019 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. 2 Link to comment Share on other sites More sharing options...
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