Jump to content

Configurable module default values on install


gRegor
 Share

Recommended Posts

First, I am running PW 2.4.12, so I realize this might be a beta bug that is fixable by me upgrading, but I'm following examples that have been around for a while, so I figured I would ask.

I am following the wiki's example for making a configurable module and it creates the fields properly, but is not storing the default values on install.

In the __construct() method I have tried both variants:

# variant 1
$this->foo = 'value';

# variant 2
$this->set('foo', 'value');

And then my getModuleConfigInputFields(array $data) method has:

$inputfields = new InputfieldWrapper();

$field = wire('modules')->get('InputfieldText');
$field->name = 'foo';
$field->label = 'Label for foo';

if ( isset($data['foo']) )
{
	$field->value = $data['foo'];
}

$inputfields->add($field);

return $inputfields;

After install, the module shows the field, but its value is blank. I've confirmed the 'data' field in the modules table is blank, too. At this point, I can enter a value, click submit, and the 'data' field is updated and reflects in the form.

Is there an additional method call needed in my __install() to trigger storing these default values?

Link to comment
Share on other sites

I think I figured this out. I thought getModuleConfigInputFields() was somehow being passed the values set in __construct(), but it's not. This method is just determining what fields will be shown on the module settings page; values are not stored in the database until the first time you click 'submit'.

If a value exists in the database, then $this->foo will contain that value. Otherwise it will default to what's set in the __construct(). I was getting caught up because I thought the default values had to be in the database before the module would let me use them.

An important thing to do, to avoid accidental blank values in the database, is ensure getModuleConfigInputFields() displays the default values if none are in the database yet. I took some code from this post to achieve that, merging the default values with the supplied $data array. In array_merge(), values in the latter arrays always take precedence.

$data = array_merge(self::getDefaultData(), $data);

I am still curious how I might store the default options in the database during install, though. Particularly if I wanted to go the second route I mentioned in the other thread. In that instance, during installation the module would loop through until a unique field name was found, and would need to store that name in the module options.

Link to comment
Share on other sites

On the point that you know there's no data in the module configuration (db) you could save it your self.

$this->modules->saveModuleConfigData($this->className(), array('my_key' => 'my_value'));

On this module (not listed in the modules dir) I have 'factory defaults'. When values are not in DB, it'll store the factory defaults.

  • Like 1
Link to comment
Share on other sites

.......I am still curious how I might store the default options in the database during install, though. Particularly if I wanted to go the second route I mentioned in the other thread. In that instance, during installation the module would loop through until a unique field name was found, and would need to store that name in the module options.

public static function getDefaultData() {
	
	return array(
			'total' => 0,
			'limit' => 10,
			'anotherDefault' => 'blah blah',

	);
}

public function ___install() {

	//do stuff here, etc., etc.

	//save default module configurations on install
	wire('modules')->saveModuleConfigData($this, self::getDefaultData());	

}
  • Like 5
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...