In user story format: As a module developer, I want to define my config in a PHP array (which I know how to write), so that I don't have to fiddle with XML or YAML or whatever.

I don't know exactly how this would work, but I would expect it to take an associative array as input, and output a config file (as a textarea or a downloadable file or whatever). Whatever we end up with, we also need a drush task for it. :)

I know this is the kind of tool we have traditionally stuck in devel, not core. I'm not convinced this belongs in core, but if it goes in devel the people who need it most may not find out about it. Maybe it should be a tool on drupal.org instead. I'm filing it against core so we can think through the best solution, and then we can move the issue wherever it belongs.

Comments

acrollet’s picture

Summary of a discussion at the Drupalcon Denver code sprint with ksenzee and beejeebus:

There should definitely be a drush command that provides this functionality, but it cannot be provided with drupal core, save a change in policy. There is a drush branch extant with replacements for vset and vget, work for drush should probably be done there.

One possibility for an interface is a simple key/value form with "add another" functionality (available under config -> development) that would let developers input their configuration settings, hit submit, and download an xml file in the proper format. There is some question whether this more properly belongs in

a) core
b) devel.module
c) someplace else.

This decision should probably be made by persons higher up the food chain :). (Or at least as a community-led decision, made after more discussion in this issue)

David_Rothstein’s picture

Title: don't make module developers write their XML by hand » Don't make module developers write XML by hand when defining default configuration

Is there a reason we can't just let module developers ship the default config via PHP code in the first place?

In addition to being simpler, I don't really understand how it would actually work to ask module developers to ship an XML file. When I look at how the variable system is used throughout core, for example, I find code like this (in menu_set_active_menu_names()):

    $active = variable_get('menu_default_active_menus', array_keys(menu_list_system_menus()));

Or this (in file_icon_path()):

    $icon_directory = variable_get('file_icon_directory', drupal_get_path('module', 'file') . '/icons');

If we assume those variables are going to be converted to the config system, then I don't see how the default values would be represented as static XML. In the latter case it might even be impossible; in the first case it's possible but poor developer experience (menu_list_system_menus() is just a static array, but it's a helper function that is used a number of other places so it would be unfortunate if its contents had to be separately duplicated as XML).

marcingy’s picture

Issue tags: +Configuration system

Tagging

sun’s picture

Category: feature » support
Status: Active » Fixed

Simple:

$file = new Drupal\Core\Config\FileStorage('mymodule.settings');
$file->write(array(
  'foo' => 'bar',
  'default' => 'setting',
));

The file is written into your site's actual config directory, but from there, you can simply copy it into your module's config directory.

If want to script that, then even that is possible:

copy($file->getFileName(), drupal_get_path('module', 'mymodule') . '/config');

Lastly, if you don't want to write, but only encode into the file format of the file storage (YAML):

$yaml = Drupal\Core\Config\FileStorage::encode(array(
  'foo' => 'bar',
  'default' => 'setting',
));

As you can see, everything is possible, very easily.


re: #2:

Separate issue. But anyway; in both cases, you simply fall back to the default programmatic result/value after checking for whether configuration exists:

$value = config('file.settings')->get('directory.icon');
$icon_directory = (!empty($value) ? $value : drupal_get_path('module', 'file') . '/icons');

Automatically closed -- issue fixed for 2 weeks with no activity.