The configuration management system in Drupal 8 seems like a great idea, but what is it? In this session I hope to show what configuration management is, why it is needed and how it will make all of our lives as Drupal site builders better.
17. Challenge
• Developer:
• Wants to work on code
• Wants to change the config
• Wants to deploy across environments
• Client:
• Wants to work on content
• Doesn’t want to lose work
19. Drupal 7 Configuration
• Features
• Install/Update hooks
• Install Profiles
How do we currently manage configuration in Drupal?
20. Features
• Export and import of certain configurations as
modules
• Not all modules supported so there tends to be
gaps in the configuration
• Adding Features support for modules is not easy
• Features ‘Overridden’ sadness
22. Install Profiles
• Combine Features modules and install hooks
• Full site setup out of the box
• Limited Drupal API available
• Doesn’t always work as planned
26. CMI
• Lead by Greg Dunlap - (heyrocker)
Also involved are:
• Joel Farris (senpai)
• Jonathan Lambert (jlambert)
• David Strauss (davidstrauss)
• Larry Garfield (crell)
• Karoly Negeyesi (chx)
• Angie Byron (webchick)
• Alex Pott (alexpott)
• And many others…
27. CMI
To build Drupal from the ground up to be better at
configuration management
28. CMI
• Move configuration management into core
• Allow storage of configuration in files
• Allow the transfer of configuration between
environments
• Create an API to allow custom configurations
• Integrate UUID into core so certain configurations
can be given machine names
45. Behind The Scenes
• Active configuration is stored in the database
• Clicking Export collates the configuration that each
module defines and combines it with the current
active configuration
• Export contains the active configuration in the form
of YAML files
46. Behind The Scenes
• YAML files are used to store the configuration
• A ‘staging’ and an ‘active’ directory are defined in
settings.php
• Used to store and compare the current
configuration
• By default the directories are stored in the location
/sites/default/files/config_<hash>/
47.
48. active
• Originally intended to be used as the location of the
active configuration
• Active configuration is now stored in the database
• Configuration development module (https://
drupal.org/project/config_devel)
49. staging
• Stores current imported configuration as YAML files
• Shows difference between configuration changes
• Changes made here can be imported into active
configuration
51. YAML
• Data orientated format
• Has several benefits over other formats like INI,
JSON, XML
• Support for hierarchical values
• Lists and associative arrays possible
• Used extensively in Drupal 8
52. YAML
• Simple values defined by
key: value
• Hierarchical values defined with whitespace
keys:
key1: value
key2: value
58. Configuration Schema
• Needed to define what your configuration will hold
• Kwalify inspired format
• Used to define what types of data the configuration
will contain
• See more at: https://drupal.org/node/1905070
60. system.schema.yml
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
Schema name
Also shows the
configuration
filename
“system.site.yml”
Used to reference
this configuration
61. system.schema.yml
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
Container
data type
‘mapping’ is for
key value sets
allows for
associative arrays
of different data
types
64. Simple Configuration
• Can be single values or arrays of values
• Used to store global configuration options
• Easy to implement:
• Create schema YAML file in
< module >/config/install/schema
• Create config YAML file
< module >/config/install
69. Configuration Entities
• Used to store custom entity configurations
• More complex and therefore harder to implement
• Used for configurations that have multiple entries
Example: Views, Image cache settings, Contact
form categories
70. Contact Category Interface
namespace Drupalcontact;
!
use DrupalCoreConfigEntityConfigEntityInterface;
!
/**
* Provides an interface defining a contact category entity.
*/
interface CategoryInterface extends ConfigEntityInterface {
!
}
75. Drush
• Full Drush integration available
• Need Drush version 7.x
• Currently in dev branch of Drush
• List of Drush commands:
http://www.drushcommands.com/drush-7x/config/
config-list
76. Drush
Export config from the active configuration to the
staging directory
drush config-export
drush cex
78. Workflow
• Staging config should become part of your
codebase
• New configuration changes should be exported
and integrated into code base
• Configuration in code should then be used to move
configuration between servers
82. MyModule
• A module that integrates with the configuration
management API
• Defines a single configuration item
• Defines a page to display the configuration
• Defines a form to change the configuration
83. MyModule Configuration
• mymodule.info.yml file defines the module
• mymodule.settings.yml contains the module
configuration
• mymodule.schema.yml contains information about
the configuration item being defined
• mymodule.routing.yml defines routes to a page and
a form
88. MyModule Page
• mymodule.routing.yml defines the route to the page
• Destination MyModuleController::description()
loads the configuration and displays it
89. namespace DrupalmymoduleController;
!
use DrupalCoreControllerControllerBase;
!
/**
* Custom page.
*/
class MyModuleController extends ControllerBase {
!
/**
* An example page.
*
* @return string
* A render array containing some page content.
*/
public function description() {
!
$mymodule_config = Drupal::config('mymodule.settings');
!
$output = array();
!
$output['mymodule_output'] = array(
'#markup' => t('The value of configitem is "!configitem"', array('!configitem' =>
$mymodule_config->get('configitem')))
);
!
return $output;
}
}
lib/Drupal/mymodule/Controller/MyModuleController.php
90. MyModule Form
• mymodule.routing.yml defines the route to the form
• Class MyModuleAdminForm defines a form that
loads the configuration
• Configuration is saved upon submission of the form
91. namespace DrupalmymoduleForm;
!
use DrupalCoreFormConfigFormBase;
!
/**
* Configure MyModule admin settings for this site.
*/
class MyModuleAdminForm extends ConfigFormBase {
!
/**
* Implements DrupalCoreFormFormInterface::getFormID().
*/
public function getFormID() {
return 'mymodule_edit_form';
}
!
/**
* Implements DrupalCoreFormFormInterface::buildForm().
*/
public function buildForm(array $form, array &$form_state) {
$config = $this->configFactory()->get('mymodule.settings');
!
$form['configitem'] = array(
'#type' => 'textfield',
'#title' => t('Config Settings'),
'#default_value' => $config->get('configitem')
);
!
return parent::buildForm($form, $form_state);
}
!
/**
* Implements DrupalCoreFormFormInterface::submitForm().
*/
public function submitForm(array &$form, array &$form_state) {
$config = $this->configFactory()->get('mymodule.settings');
!
$config->set('configitem', $form_state['values']['configitem'])->save();
!
parent::submitForm($form, $form_state);
}
}
lib/Drupal/mymodule/Form/MyModuleAdminForm.php