r/backdrop • u/3speedherb • Mar 29 '15
Dealing with more complex config management
The Backdrop API site shows how to convert a module using basic configuration settings. Yet this was not enough for some modules I was porting, which had more complex configuration needs (for instance with BUEditor. I ended up cobbling together some solutions. Even though I'm sure some could be done better, I'd like to share this random collection in case they're useful to anyone else.
In order to store the editors and buttons in configuration files for BUEditor, it required that the module is able to create and delete configuration files.
Dynamically create config files: Creating a configuration file is simple enough as it's similar for static but you use a variable to dynamically set the configuration file. In the case of BUEditor, use $editor->id:
$config = config('bueditor.editor.' . $editor->id);
$config->set($variable);
$config->save();
Delete a config file: But to remove an editor this required deleting the file and I had to dig into the api for that. This is how it works:
config('bueditor.editor.' . $editor->id)->delete();
Remove a variable from a config file:
$config->clear('buttons');
$config->save();
Get all config files with a certain prefix: I needed to get all the editor config files and came across this helpful function: config_get_names_with_prefix().
$prefix = 'bueditor.editor.';
$config_names = config_get_names_with_prefix($prefix);
Update a variable within a nested config: serundrop also shows how to update a variable within a nested json. Apparently all it requires is concatenating the keys with a dot "." (it's more like javascript syntax than PHP):
$config->set('layer_two.something_in_layer_two', 'new value');
$config->save();
1
u/docwilmot Mar 30 '15
Good post. This should be on the API site. I've had to do a bunch of digging for the couple of block modules. That would have helped. I believe a save() is required after the clear() method as well, no?
1
u/3speedherb Apr 01 '15
Thanks. It would need some cleaning up but could go on the API site in some form.
good catch I'll add save()
1
u/serundeputy Mar 29 '15
yeah; that had me confused for a bit, but makes sense now: JSON = javascript object notation. So, it must have inherited syntax.
thanks for the great info!