Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit dfb3c0e

Browse files
Edward Wugrasmash
Edward Wu
authored andcommitted
Update configuration-management.md (#2051)
Fixes #1977: Provide example of importing individual configuration file from default.
1 parent 4385f1f commit dfb3c0e

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

readme/configuration-management.md

+48
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,54 @@ However, BLT does not create any configuration splits for you. For detailed info
9999

100100
If for some reason BLT is not working with Config Split, ensure that you are using Drush version 8.1.10 or higher, Config Split version 8.1.0-beta4 or higher, and that `cm.strategy` is set to `config-split` in `blt/project.yml`.
101101

102+
### Using update hooks to importing individual config files
103+
104+
BLT runs module update hooks before importing configuration changes. For use cases where it is necessary for a configuration change to be imported before the update hook runs, in your hook, you'll need to import the needed configuration from files first. (An example of this would be adding a new taxonomy vocabulary via config, and populating that vocabulary with terms in an update hook.)
105+
106+
Code snippet for importing a taxonomy vocabulary config first before creating terms in that vocabulary:
107+
108+
use Drupal\taxonomy\Entity\Term;
109+
110+
// Import taxonomy from config sync directory.
111+
$vid = 'foo_terms'; // foo_terms is the vocabularly id.
112+
$vocab_config_id = "taxonomy.vocabulary.$vid";
113+
$vocab_config_data = foo_read_config_from_sync($vocab_config_id);
114+
\Drupal::service('config.storage')->write($vocab_config_id, $vocab_config_data);
115+
116+
Term::create([
117+
'name' => 'Foo Term 1',
118+
'vid' => $vid',
119+
])->save();
120+
121+
Term::create([
122+
'name' => 'Foo Term 2',
123+
'vid' => $vid',
124+
])->save();
125+
126+
This depends on a helper function, which can be added to your custom profile:
127+
128+
use Drupal\Core\Config\FileStorage;
129+
130+
/**
131+
* Reads a stored config file from config sync directory.
132+
*
133+
* @param string $id
134+
* The config ID.
135+
*
136+
* @return array
137+
* The config data.
138+
*/
139+
function foo_read_config_from_sync($id) {
140+
// Statically cache FileStorage object.
141+
static $storage;
142+
143+
if (empty($storage)) {
144+
global $config_directories;
145+
$storage = new FileStorage($config_directories[CONFIG_SYNC_DIRECTORY]);
146+
}
147+
return $storage->read($id);
148+
}
149+
102150
## Features-based workflow
103151

104152
Features allows you to bundle related configuration files (such as a content type and its fields) into individual feature modules. Drupal treats features just like normal modules, but Features and its dependencies add some special sauce that allow features to not only provide default configuration (like normal modules), but to also update (track and import) changes to this configuration.

0 commit comments

Comments
 (0)