Problem/Motivation

Currently the module can only write back configuration on ConfigEvents::SAVE and ConfigEvents::RENAME
and configuration export settings are globally defined in the config_devel settings.
Currently there is no way to write back configuration to a specific modules config install directory without changing the system permissions.

Proposed resolution

Allow the developer to specify the configuration he wants to write back to his module as part of its info file.
Add a drush command that reads the info file and writes back the configuration to the modules config/install directory

$ drush help config-writeback
Write back configuration to module's config/install directory.
List which configuration settings you want to export in the module's info file by listing them under 'config_devel', as shown below:

config_devel:
  - entity.view_display.node.article.default
  - entity.view_display.node.article.teaser
  - field.instance.node.article.body


Examples:
 drush config-writeback MODULE_NAME        Write back configuration to the specified module, based on .info file.

Arguments:
 module                                    Module machine name.

Aliases: cwb

Remaining tasks

none

User interface changes

none

API changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

ademarco’s picture

ademarco’s picture

Title: Add Drusch command for exporting config files » Add Drush command for exporting config files
bircher’s picture

Status: Active » Needs review
chx’s picture

Status: Needs review » Needs work
+          try {
+            $service->write($name, $data);
+          } catch (StorageException $e) {          
+          }

catch goes on a new line.

Also, it seems to me that there's an excellent opportunity of code reusal here. Say, change autoExportConfig to:

    $auto_export = $this->configFactory->get('config_devel.settings')->get('auto_export');
    $file_names = array_keys(array_intersect($auto_export, array($config_name)));
    $this->writeBackConfig($config, $file_names);

then you could just reuse that method \Drupal::service('config_devel.writeback_subscriber')->writeBackConfig. This would fix a bug in the patch which unset uuid unconditionally; it should only be unset for config entities but that is handled neatly in the code (which I have neatly stolen from beejeebus :) )

bircher’s picture

code re-use is a good idea!
Attached is the new patch.

bircher’s picture

Status: Needs work » Needs review
chx’s picture

Nifty. I will commit this later; but I will change the direct info file reading and parsing to InfoParser::parse but I can do that myself.

bircher’s picture

Nice!
InfoParser::parse would be used along the lines of the attached patch?
There is still plenty of things to learn for me :)

chx’s picture

I am learning myself. I think it's \Drupal::service('info_parser')->parse($filename).

bircher’s picture

pescetti’s picture

We can avoid hardcoded values by using system_get_info() and constants defined in InstallStorage. New iteration attached.

pescetti’s picture

Reordered files so that the patch in #11 is displayed in the table at the top.

chx’s picture

Erm, nope. system_get_info rebuilds the whole module data. I am not shooting down a whole herd because I want a steak :D but feel free to tell me otherwise.

pescetti’s picture

Re-rolled patch by removing the improvement that looks too expensive for us (i.e., system_get_info() call) and keeping the other one (constants defined in InstallStorage). Or is this one too expensive too?

chx’s picture

One last question: how do I commit a patch like this; I can only put one of you as --author.

pescetti’s picture

It's joint work (we are all colleagues) and you can consider bircher to be the main author for attribution purposes.

  • chx committed b59fcff on 8.x-1.x authored by bircher
    Add Drush command for exporting config files #2300717 by bircher,...
chx’s picture

Status: Needs review » Needs work

Sorry for the delay, I wanted to have tests in place first. I committed this as it stands but I can't guarantee this will always work :/ I would recommend some phpunit test that tests writeBackConfig(). https://www.drupal.org/node/2294439 looks useful too.

bircher’s picture

Status: Needs work » Needs review
FileSize
1.51 KB

Attached is the patch for the tests rewritten to use vfsStream.

maybe the path of the filenames could now be simplified, but the tests seem to pass on my system.
lets see what the bot thinks about it.

bircher’s picture

ah it will likely fail since I also included the patch from https://www.drupal.org/node/2315187 which is needed for the module to run at all.

chx’s picture

Status: Needs review » Needs work

+ public function setUp()
+ {

should be

+ public function setUp() {

fixed and committed. We still need that unit test though :)

  • chx committed 134c786 on 8.x-1.x authored by bircher
    converted tests to use vfsStream by bircher #2300717
    
bircher’s picture

Status: Needs work » Needs review
FileSize
3.51 KB

added unit tests for writeBackConfig()

chx’s picture

Status: Needs review » Fixed

Thanks so much! That will make sure the drush command can call it happily even as we go forward.

  • chx committed 3619d82 on authored by bircher
    Added unit tests for writeBackConfig #2300717 by bircher
    
chx’s picture

Category: Feature request » Task
Status: Fixed » Active

Please rename the command to config_export_per_info or something. Introducing new words like 'writeback' is confusing. Let's not become features again. Please provide a config_import_per_info which does the same in the opposite direction.

Iztok’s picture

Attaching the patch that renames the command as discussed on IRC with chx.

chx’s picture

Status: Active » Needs review

I like this but I will wait for bircher's feedback.

The last submitted patch, 8: config_devel-drush-config-writeback-command-2300717-8.patch, failed testing.

The last submitted patch, 10: config_devel-drush-config-writeback-command-2300717-10.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 27: config_devel-renaming_drush_command-2300717-27.patch, failed testing.

Status: Needs work » Needs review
bircher’s picture

Hi,
there is no objection for a more descriptive name.
we took writeback because that name was used internally, but that alone is no justification.

Thanks for the better name

bircher’s picture

FileSize
1.38 KB
538 bytes
No hook functions were found for config-devel-export-module. The primary hook function is                    [error]
drush_config_devel_export_module(). Please implement this function. Run with --show-invoke to see all
available hooks.

I am using the latest drush, so the function should have the suggested name.

chx’s picture

Status: Needs review » Fixed

Thanks for catching that, committed.

  • chx committed 71c5935 on 8.x-1.x authored by Iztok
    Rename drush command from cwb to cd-em for more CMI consistent naming #...

Status: Fixed » Closed (fixed)

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