Skip to content

Commit 11ed42f

Browse files
bircherweitzman
authored andcommitted
Add storage filter hook for benefit of config_split and similar contrib/custom projects.
This brings back commit 7322efc.
1 parent 0f18539 commit 11ed42f

File tree

5 files changed

+106
-13
lines changed

5 files changed

+106
-13
lines changed

commands/core/config.drush.inc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,14 +591,16 @@ function drush_config_import_validate() {
591591
}
592592

593593
/**
594-
* Presently, the only configuration storage filter that is supported
595-
* is the 'CoreExtensionFilter'. If other use cases arise that are
596-
* not supported by Drupal's configuration override system, then we
597-
* could add a hook here via drush_command_invoke_all('drush_storage_filters');
598-
*
599-
* See: https://github.com/drush-ops/drush/pull/1522
594+
* Return storage filters to alter config import and export.
600595
*/
601596
function drush_config_get_storage_filters() {
597+
return drush_command_invoke_all('drush_storage_filters');
598+
}
599+
600+
/**
601+
* Implements hook_drush_storage_filters().
602+
*/
603+
function config_drush_storage_filters() {
602604
$result = array();
603605
$module_adjustments = drush_get_option('skip-modules');
604606
if (!empty($module_adjustments)) {

docs/config-exporting.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ values, and how to make more complex changes.
1515

1616
It is not necessary to alter the configuration system values to
1717
make simple value changes to configuration variables, as this may be
18-
done by the configuration override system.
18+
done by the [configuration override system](https://www.drupal.org/node/1928898).
1919

2020
The configuration override system allows you to change configuration
2121
values for a given instance of a site (e.g. the development server) by
@@ -24,11 +24,6 @@ For example, to change the name of a local development site:
2424
```
2525
$config['system.site']['name'] = 'Local Install of Awesome Widgets, Inc.';
2626
```
27-
If you wish to change configuration values in code rather than in
28-
your settings.php file, it is also possible to alter configuration
29-
values in module hooks. See the [configuration override system](https://www.drupal.org/node/1928898)
30-
documentation for details.
31-
3227
Note that the configuration override system is a Drupal feature, not
3328
a Drush feature. It should be the preferred method for changing
3429
configuration values on a per-environment basis; however, it does not
@@ -56,3 +51,11 @@ will not cause it to be disabled again. Similarly, if you make changes
5651
to configuration on the development environment and export them, then
5752
the devel module will not be listed in the exports.
5853

54+
## More Complex Adjustments
55+
56+
Drush allows more complex changes to the configuration data to be made
57+
via the configuration filter mechanism. In order to do this, you must
58+
write some code inside a Drush extension.
59+
60+
See [Drupal Configuration Filtering](config-filter.md) for more information
61+
on how to do this.

docs/config-filter.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Filtering Drupal Configuration
2+
3+
When exporting and importing configuration from and to a Drupal 8 site,
4+
Drush provides a mechanism called the Configuration Filter system which
5+
allows configuration values to be altered during import and export, allowing
6+
you to vary your configuration by environment. The --skip-modules option
7+
in the config-import and config-export commands is implemented with a
8+
configuration filter. For more complex uses, you will need to write some
9+
custom code.
10+
11+
## Other Alternatives
12+
13+
The Drupal Configuration system provides the capability to [add configuration
14+
overrides from modules](https://www.drupal.org/node/1928898). Configuration
15+
overrides should be provided from a module override when possible. Implementing
16+
an override via a Drush extension is convenient in situations where you would
17+
like to be able to pass values in to the configuration filter via a Drush
18+
commandline option.
19+
20+
## Filtering Drupal Configuration with Drush
21+
22+
Instructions on writing a Drush extension to filter Drupal configuration follows.
23+
24+
### Getting started
25+
26+
The first thing that you will need to do is set up a Drush extension
27+
to hold your storage filter hook. See the example
28+
[example sandwich commandfile](../examples/sandwich-drush.inc) for
29+
details; note, however, that it is not necessary for your commandfile
30+
to implement hook_drush_command(), or any other hook besides the storage
31+
filter hook.
32+
33+
You will need a composer.json file as well, in order to define where
34+
your StorageFilter class is defined. Make sure that Drush and your
35+
custom commandfile are required from the composer.json file of any
36+
Drupal site that you plan on using your filter with.
37+
38+
### Implementing the Storage Filter Hook
39+
40+
When Drush imports or exports configuration, it gives all Drush
41+
extensions a chance to hook this process by way of the hook
42+
hook_drush_storage_filters. The implementation of this hook,
43+
in the file MYFILTER.drush.inc, would look like this:
44+
```
45+
function MYFILTER_drush_storage_filters() {
46+
$result = array();
47+
$my_option = drush_get_option('my-option');
48+
if (!empty($my_option)) {
49+
$result[] = new MyConfigurationFilter($my_option);
50+
}
51+
return $result;
52+
}
53+
```
54+
With this hook in place, MyConfigurationFilter will become part of
55+
the import / export process.
56+
57+
### Implementing a Storage Filter
58+
59+
It is necessary to implement a class that implements
60+
[StorageFilter](https://github.com/drush-ops/drush/blob/master/lib/Drush/Config/StorageFilter.php).
61+
Your class only needs to implement the two methods defined there,
62+
filterRead() and filterWrite(), to make whichever alterations to configuration
63+
you need during the export and import operations, respectively. For
64+
an example class that implements StorageFilter, see the
65+
[CoreExtensionFilter](https://github.com/drush-ops/drush/blob/master/lib/Drush/Config/CoreExtensionFilter.php)
66+
class.

drush.api.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function hook_drush_command() {
1414
}
1515

1616
/**
17-
* All drush commands are invoked in a specific order, using
17+
* All Drush commands are invoked in a specific order, using
1818
* drush-made hooks, very similar to the Drupal hook system. See drush_invoke()
1919
* for the actual implementation.
2020
*
@@ -420,6 +420,22 @@ function hook_drush_invoke_alter($modules, $hook) {
420420
}
421421
}
422422

423+
/*
424+
* Storage filters alter the .yml files on disk after a config-export or before
425+
* a config-import. See `drush topic docs-config-filter` and config_drush_storage_filters().
426+
*/
427+
function hook_drush_storage_filters() {
428+
$result = array();
429+
$module_adjustments = drush_get_option('skip-modules');
430+
if (!empty($module_adjustments)) {
431+
if (is_string($module_adjustments)) {
432+
$module_adjustments = explode(',', $module_adjustments);
433+
}
434+
$result[] = new CoreExtensionFilter($module_adjustments);
435+
}
436+
return $result;
437+
}
438+
423439
/**
424440
* @} End of "addtogroup hooks".
425441
*/

mkdocs.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ pages:
2121
- Command Authoring: commands.md
2222
- Bootstrap: bootstrap.md
2323
- Context system: context.md
24+
- Filtering Drupal configuration: config-filter.md
25+
theme: readthedocs
26+
site_author: ""
27+
repo_url: https://github.com/drush-ops/drush
28+
include_search: true
29+
#use_directory_urls: false

0 commit comments

Comments
 (0)