Hi all,

I'm working on a couple of projects, porting them to Backdrop, and I ran into a bit of a unique challenge. It appears that the module I'm working on uses dynamic names when using variable_get(). To convert this to use config_get(), I need to put the default value into the .settings.json file as explained here. How should I approch this?  The  code in question looks like so:

$type_menus = variable_get('menu_options_' . $params['type'], array('main-menu' => 'main-menu'));

As far as I have read, JSON doesn't allow for wildcards. Has anyone else run into this? How did you solve it?

Comments

BWPanda's picture

Yep, I've come across this before. Can't remember what module though...

The solution is to put all non-dynamic ('static' I guess) variables in your default config file as normal, then add the dynamic ones via hook_install() in your .install file (using config_set()).

For example:

my_module.settings.json:

{
    "_config_name": "my_module.settings",
    "menu_options_type1": {
      "main-menu": "main-menu"
    },
    "menu_options_type2": {
      "main-menu": "main-menu"
    },
    "menu_options_type3": {
      "main-menu": "main-menu"
    }
}

or my_module.install:

/**
 * Implements hook_install().
 */
function my_module_install() {
...
  config_set('my_module.settings', 'menu_options_' . $params['type'], array('main-menu' => 'main-menu'));
}