Jump to content

Array in module settings


Rudy
 Share

Recommended Posts

Hi,

I am writing a custom module that requires storing array of settings in the module config/settings.

Is there a built-in fieldtype that allows me to store settings in an array (sort of like Repeater)?

I tried using InputfieldAceExtended and InputfieldTextarea to store JSON array. Both times, my data was stored in the module settings (in the database) but upon reload, that information was not retrieved.

Thanks
Rudy

 

Link to comment
Share on other sites

Hi Rudy,

 

Storing a JSON array in a TextArea should works as expected without doing anything than saving the field in a standard fashion. 

 

protected static function getDefaultData()
{
    	return array(
        	'json_config'  => ''
    	);
}

public static function getModuleConfigInputfields(array $data) {

        $data = array_merge(self::getDefaultData(), $data);
        $modules = wire('modules');

        $fields = new InputfieldWrapper();

        $field = $modules->get("InputfieldTextarea");
        $field->label = __('JSON Config');
        $field->attr('name+id', 'json_config');
        $field->attr('value', $data['json_config']);
        $fields->append($field);

		return $fields;
}

 

  • Like 3
Link to comment
Share on other sites

13 hours ago, Rudy said:

Both times, my data was stored in the module settings (in the database) but upon reload, that information was not retrieved.

The fact that the data is being stored in the database as json is good. On retrieval you need to explicitly tell PW what/where to put the data. For example, a textarea field only has one 'value'. In your config screen there a several fields which should be populated with bits of the json data. I think you're one step away. Retrieve the json then assign the individual bits to your module config inputfields

  • 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

×
×
  • Create New...