Simplify Drupal Configuration Management using Drush - Bhimmu
Configuration is the soul of a Drupal application that can be stored as part of your codebase. Elevate Drupal configuration management using Drush Commands to save countless hours of rework.
What is a configuration management in Drupal?
In Drupal, configuration refers to a collection of admin settings that store everything related to the site, such as content type, taxonomy, user entities, and their fields.
Settings forms and their values include fields label, text and translations, a list of modules enabled, and many more. It is a process of syncing site configuration from one environment to another to avoid rework.
For example, you are working on a feature that requires one text field in the Basic Page content type. You can add this field to the local dev environment and perform unit testing, but to assign the task to the QA team, you must create this field in the stage environment.
Do you want to repeat the process there again? What if you have to do this same task in three different environments?
Here, you should leverage Drupal Configuration Management to make your life easy.
Configuration can be exported in the form of YAML files and stored in a folder. This folder can be added to the Git version control so that you can deploy configuration files with the codebase.
How to setup a config sync directory
It is recommended to save the configuration outside the Drupal root directory to make them secure. Sync directory can be defined in the settings.php file.
# web/sites/default/settings.php
$settings['config_sync_directory'] = '../config/sync';
Drupal Configuration Management using Drush
Drush is a command line shell for Drupal to perform administrative tasks. Here are most useful commands to work with Configuration Mangement.
- drush config:status
- drush config:export
- drush config:import
- drush config:pull
- drush config:get
- drush config:set
- drush config:delete
- drush config:edit
drush config:status
Display status of configuration (differences between the filesystem configuration and database configuration).
Examples:
drush config:status Display configuration items that need to be synchronized.
drush config:status --state=Identical Display configuration items that are in default state.
drush config:status --state='Only in sync dir' Display all content types that would be created in active storage
--prefix=node.type. on configuration import.
drush config:status --state=Any --format=list List all config names.
drush config:status 2>&1 | grep "No Check there are no differences between database and exported
differences"
Aliases: cst, config-status
drush config:export
Export Drupal configuration to a directory.
Examples:
drush config:export Export configuration files to the site's config directory.
drush config:export --destination Export configuration; Save files in a backup directory named config-export.
Aliases: cex, config-export
drush config:import
Import config from the config directory.
Examples:
drush config:import Update Drupal's configuration so it matches the contents of the
config directory.
drush config:import --partial Import from the /app/config directory which typically contains
--source=/app/config one or a few yaml files.
cat tmp.yml | drush config:set Update the user.mail config object in its entirety.
--input-format=yaml user.mail ? -
Aliases: cim, config-import
drush config:pull
Export and transfer config from one environment to another.
Examples:
drush config:pull @prod @stage Export config from @prod and transfer to @stage.
drush config:pull @prod @self:../config/sync Export config and transfer to a custom directory. Relative paths are
calculated from Drupal root.
Arguments:
source A site-alias or the name of a subdirectory within /sites whose config you want to copy from.
destination A site-alias or the name of a subdirectory within /sites whose config you want to replace.
Aliases: cpull, config-pull
drush config:get
Display a config value, or a whole configuration object.
Examples:
drush config:get system.site Displays the system.site config.
drush config:get system.site page.front Gets system.site:page.front value.
Arguments:
config_name The config object name, for example system.site.
[key] The config key, for example page.front. Optional.
Aliases: cget, config-get
drush config:set
Save a config value directly. Does not perform a config import.
Examples:
drush config:set system.site name MySite Sets a value for the key name of
system.site config object.
drush config:set system.site page.front Sets the given URL path as value for the config item with key
/path/to/page page.front of system.site config
object.
drush config:set system.site '[]' Sets the given key to an empty array.
drush config:set system.site 'NULL' Sets the given key to NULL.
drush config:set --input-format=yaml Use a sequence as value for the key permissions of
user.role.authenticated permissions [foo,bar] user.role.authenticated config object.
drush config:set --input-format=yaml system.site Use a mapping as value for the key page of
page {403: '403', front: home} system.site config object.
drush config:set --input-format=yaml Update two top level keys (label, weight) in the
user.role.authenticated ? "{label: 'Auth user', system.site config object.
weight: 5}"
cat tmp.yml | drush config:set Update the user.mail config object in its entirety.
--input-format=yaml user.mail ? -
Arguments:
config_name The config object name, for example system.site.
key The config key, for example page.front. Use ? if you are updating multiple
top-level keys.
value The value to assign to the config key. Use - to read from Stdin.
Aliases: cset, config-set
drush config:delete
Delete a configuration key, or a whole object(s).
Examples:
drush config:delete system.site,system.rss Delete the system.site and system.rss config objects.
drush config:delete system.site page.front Delete the 'page.front' key from the system.site object.
Arguments:
config_name The config object name(s). Delimit multiple with commas.
[key] A config key to clear, May not be used with multiple config names.
Aliases: cdel, config-delete
drush config:edit
Open a config file in a text editor. Edits are imported after closing editor.
Examples:
drush config:edit image.style.large Edit the image style configurations.
drush config:edit Choose a config file to edit.
drush --bg config-edit image.style.large Return to shell prompt as soon as the editor window opens.
Arguments:
config_name The config object name, for example system.site.
Aliases: cedit, config-edit
How to install Drush in Drupal
Drush can be installed as a composer dependency, similar to other modules.
composer require drush/drush
Run the above command from the project root directory, where your composer.json file lives.
How to start Drupal Configuration Management using Drush
Open your terminal and navigate to the project directory. In my case /var/www/bhimmu is the project root directory.
- Export the configuration foe the first time
vendor/bin/drush config:export
Above command will export the active configuration in your config sync directory. Add config sync directory to your git version control. Now add some new fields to the basic page content type to see the difference.
Check the difference in the active configuration and the stage configuration
vendor/bin/drush config:status
If you see the changes, export them again to the sync folder.
vendor/bin/drush config:export
Above command will add the exported configuration to the sync folder. Commit these changes and push your code the dev environment. Run the below command once code deployed to the dev environment. You can run below command on dev server by logging via ssh or you can set site aliases.
Using site alias you can run Drush commands from your local env to any remote env
vendor/bin/drush @bhimmu.dev config:import
Clear the cache and verify the new fields are available without adding them again to the dev environment.
In this example we have taken only one field but in reality there can be lots of settings that you may need to implement in all the Env. Drupal configuration management using drush can perform this activity correctly and save time, reduce post deployment activities and bugs.