Gadgetto Posted May 4, 2019 Share Posted May 4, 2019 Hi, while working on 2 new configurable modules (with a lot of config settings) I encountered that the module config is not created/saved to db when the module is installed. You need to hit save to write the default config to database. This is a problem if I need some of these config values at modules runtime. Wouldn't it be better to save the default config values of the module on module install? Do I miss something here? Link to comment Share on other sites More sharing options...
horst Posted May 4, 2019 Share Posted May 4, 2019 (edited) As i know it and do it: having an array with the default values in the module that gets merged with the db stored data on ready, for example. No need to store it into db on install. Also think about an update of a module that has a new option implemented. With array_merge everything is up to date with the new module files. EDIT: also I‘m not sure if the system is ready to store configdata during install, or if this is only possible after the installation (registration) process. Edited May 4, 2019 by horst Link to comment Share on other sites More sharing options...
Gadgetto Posted May 4, 2019 Author Share Posted May 4, 2019 21 minutes ago, horst said: As i know it and do it: having an array with the default values in the module that gets merged with the db stored data on ready, for example. No need to store it into db on install. Also think about an update of a module that has a new option implemented. With array_merge everything is up to date with the new module files. @horst This makes sense. I'm using the MyModuleConfig.php Class method to handle module configuration. If accessing module config via $modules->getConfig('MyModule') I only get config from db. If module configuration settings aren't at least saved once I get the following results: $this->config_key --> 'DEFAULT VALUE' $data['config_key'] --> null (key doesn't exists) @ryan says in his blog post regarding "ProcessWire 2.5.5 – New module configuration" https://processwire.com/blog/posts/new-module-configuration-options/ Quote Default values get automatically populated to our module (we don't need to do that manually in a __construct() method). I thought this should be handled automatically? Where is the best place in MyModuleConfig.php Class to merge defaults with db values? In constructor? Think I still don't understand module config behavior fully ... ? Link to comment Share on other sites More sharing options...
Zeka Posted May 4, 2019 Share Posted May 4, 2019 @Gadgetto Maybe you can check if there is any information in DB and use getDefaults() method as a fallback. https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/core/ModuleConfig.php#L60 Also, take a look at the description of the getDefaults method https://github.com/processwire/processwire/blob/341342dc5b1c58012ae7cb26cffe2c57cd915552/wire/core/ModuleConfig.php#L55 2 Link to comment Share on other sites More sharing options...
Gadgetto Posted May 4, 2019 Author Share Posted May 4, 2019 1 hour ago, Zeka said: @Gadgetto Maybe you can check if there is any information in DB and use getDefaults() method as a fallback. That's what I'm doing currently. I thought there should be a more elegant way. If I try to access a module property via $this->my_property ProcessWire should decide whether the value comes from the database (if available) or from the defaults. Link to comment Share on other sites More sharing options...
horst Posted May 4, 2019 Share Posted May 4, 2019 This is old schooled from before PW 2.4, but it works fine for me. I use it until today, not only for modules that need backwards compatibility: /** * Default settings used by this module * * @return array */ static public function getDefaultData() { return array( 'myKey' => 'myValue', 'anotherKey' => 'anotherValue' ); } /** * Populate final merged data from default and DB * (use init or ready method for this, depending on your needs or preferences) */ public function ready() { $data = $this->wire('modules')->getConfig($this->className); // fetch the config data from DB for this module $data = array_merge(self::getDefaultData(), $data); // merge with default data foreach($data as $key => $value) $this->$key = $value; // populate final data to class properties } /** * the config screen: */ static public function getModuleConfigInputfields(array $data) { $data = array_merge(self::getDefaultData(), $data); // merge stored data from DB with default data // ... } 1 Link to comment Share on other sites More sharing options...
Gadgetto Posted February 25, 2020 Author Share Posted February 25, 2020 If you are using the separate ModuleConfig class method you can use it like this: // Get module config. // (Holds merged data from DB and default config. // This works because of using the ModuleConfig class) $moduleConfig = $this->wire('modules')->get('ModuleName'); // You can than access the keys like this: $value = $moduleConfig->name_of_key; 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