Drupal 8: Configuration Management
by Alex Goja
What Is Drupal Config?
A quick quiz01
What is Drupal config ?
Blog post
What is Drupal config ?
Permissions
What is Drupal config ?
Vocabulary
What is Drupal config ?
Taxonomy term
What is Drupal config ?
Field definition
What is Drupal config ?
Menu
What is Drupal config ?
Variable
What is Drupal config ?
Menu item
What is Drupal config ?
User
What is Drupal config ?
Contents of a block
Content An article page, uploaded files
Session
State
Configuration
Logged in status, shopping carts
Last cron run
Everything else
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
Current Situation
02
Current Situation
Drupal 7 Configuration
How do we currently manage configuration in Drupal?
• Features
• Install/Update hooks
• Install Profiles
Current Situation
Features
• Features ‘Overridden’ sadness
• Adding Features support for modules is not easy
• Export and import of certain configurations as modules
Current Situation
Install/Update Hooks
• Create tables

• Set up variables

• Fill in ‘gaps’ in Features modules
Current Situation
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
Drupal 8 CM
03
Drupal 8 CM
Configuration Management
• 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
Usage
04
Drupal 8 CM
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
Drupal 8 CM
Behind The Scenes
• YAML files are used to store the configuration
• Used to store and compare the current configuration
• By default the directories are stored in the location /sites/default/files/
config_<hash>/
Drupal 8 CM
YAML Filenames In Drupal 8
• < module >.< component >.yml
system.settings.yml 

views.settings.yml
• < module >.< component >.< entity >.yml
image.style.medium.yml 

views.view.content.yml
Configuration API
05
Configuration API
Configuration Schema
• Needed to define what your configuration will hold
• Used to define what types of data the configuration will contain
• See more at: https://drupal.org/node/1905070
Configuration API
system.schema.yml
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
mail:
type: email
label: 'E-mail address'
slogan:
type: label
label: 'Slogan'
page:
type: mapping
label: 'Pages'
mapping:
403:
type: path
label: 'Default 403 (access denied) page'
404:
type: path
label: 'Default 404 (not found) page'
front:
type: path
label: 'Default front page'
admin_compact_mode:
type: boolean
label: 'Compact mode'
weight_select_max:
type: integer
label: 'Weight element maximum value'
langcode:
type: string
label: 'Default language'
Configuration API
system.schema.yml
Schema name 

- Used to reference this configuration 

- Also shows the configuration filename
“system.site.yml”
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
Configuration API
Container data type
- ‘mapping’ is for key value sets
- allows for associative arrays of 

different data types
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
system.schema.yml
Configuration API
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
Label
- Used as an interface label
system.schema.yml
Configuration API
system.site:
type: mapping
label: 'Site information'
mapping:
uuid:
type: string
label: 'Site UUID'
name:
type: label
label: 'Site name'
system.schema.yml
Start of mapping section
Item key
Item type
Item label
Configuration API
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
Configuration API
Getting Configuration
uuid: ''
name: Drupal
mail: ''
slogan: ''
page:
403: ''
404: ''
front: user
admin_compact_mode: false
weight_select_max: 100
langcode: en
$config = Drupal::config('system.site');
$email = $config->get('mail');
$cofig = Drupal::config(‘system.site')
$page403 = $config->get('page.403'); 

Configuration API
Setting Configuration
$config = Drupal::config('system.site'); $config->set(‘mail’,
‘test@example.com’); $config->save();




$config = Drupal::config(‘system.site’)->set('mail', ‘test@example.com’); 

$config->save();







Drupal::config(‘system.site’)->set('mail', ‘test@example.com)- >save();
Configuration API
Clear Configuration
$config = Drupal::config('system.site');
$config->clear('mail')->save(); 



Drupal::config('system.site')->delete();
Configuration API
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
Configuration API
Contact Category Interface
namespace Drupalcontact;



use DrupalCoreConfigEntityConfigEntityInterface; 

/**
* Provides an interface defining a contact category entity.
*/
interface CategoryInterface extends ConfigEntityInterface { 

}
Configuration API
Contact Category Entity
Configuration API
class Category extends ConfigEntityBase implements CategoryInterface {
/**
* The category ID.
*
* @var string
*/
public $id;
/**
* The category label.
*
* @var string
*/
public $label;
/**
* List of recipient e-mail addresses.
*
* @var array
*/
public $recipients = array();
/**
* An auto-reply message to send to the message author.
*
* @var string
*/
public $reply = '';
/**
* Weight of this category (used for sorting).
*
* @var int
*/
public $weight = 0;
Configuration API
contact.category.personal.yml
id: personal

label: 'Personal contact form'

recipients: { }

reply: ''

weight: 0

status: true

uuid: 43155e41-8a58-4264-ab00-be97a0736aa0 langcode: en

dependencies: { }
$contact_category = $this->entityManager()
->getStorage('contact_category')
->load(‘personal');
$contact_category->label();
Configuration API
contact.category.personal.yml
id: personal

label: 'Personal contact form'

recipients: { }

reply: ''

weight: 0

status: true

uuid: 43155e41-8a58-4264-ab00-be97a0736aa0 langcode: en

dependencies: { }
$config = Drupal::config('contact.category.personal')->get();
$label = $config['label'];



$label = Drupal::config(‘contact.category.personal')->get('label');
Configuration API
Drush
Export config from the active configuration to the staging
directory
drush config-export
drush cex
Configuration API
Drush
Import the staging configuration into the active configuration
drush config-import
drush cim
Configuration API
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
Configuration API
Configuration API
Drupal 7
06
Configuration API
Drupal 7
• Configuration management has been back ported
• Configuration Management module 

https://drupal.org/project/configuration
Resources
07
Configuration API
Resources
• Creating Drupal 8.x modules 

https://drupal.org/developing/modules/8
• Configuration API in Drupal 8 

https://drupal.org/node/1667894
• Understanding Drupal 8's config entities 

http://www.previousnext.com.au/blog/understanding-drupal-8s-config-entities
• Configuration schema/metadata 

https://drupal.org/node/1905070
• Configuration inspector for Drupal 8 

https://drupal.org/project/config_inspector
Questions ?
Alex Goja
Team Lead
agoja@adyax.com

Config management

  • 1.
    Drupal 8: ConfigurationManagement by Alex Goja
  • 2.
    What Is DrupalConfig? A quick quiz01
  • 3.
    What is Drupalconfig ? Blog post
  • 4.
    What is Drupalconfig ? Permissions
  • 5.
    What is Drupalconfig ? Vocabulary
  • 6.
    What is Drupalconfig ? Taxonomy term
  • 7.
    What is Drupalconfig ? Field definition
  • 8.
    What is Drupalconfig ? Menu
  • 9.
    What is Drupalconfig ? Variable
  • 10.
    What is Drupalconfig ? Menu item
  • 11.
    What is Drupalconfig ? User
  • 12.
    What is Drupalconfig ? Contents of a block
  • 13.
    Content An articlepage, uploaded files Session State Configuration Logged in status, shopping carts Last cron run Everything else
  • 14.
    Challenge • Developer: • Wantsto 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
  • 15.
  • 16.
    Current Situation Drupal 7Configuration How do we currently manage configuration in Drupal? • Features • Install/Update hooks • Install Profiles
  • 17.
    Current Situation Features • Features‘Overridden’ sadness • Adding Features support for modules is not easy • Export and import of certain configurations as modules
  • 18.
    Current Situation Install/Update Hooks •Create tables
 • Set up variables
 • Fill in ‘gaps’ in Features modules
  • 19.
    Current Situation 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
  • 20.
  • 21.
    Drupal 8 CM ConfigurationManagement • 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
  • 22.
  • 34.
    Drupal 8 CM BehindThe 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
  • 35.
    Drupal 8 CM BehindThe Scenes • YAML files are used to store the configuration • Used to store and compare the current configuration • By default the directories are stored in the location /sites/default/files/ config_<hash>/
  • 36.
    Drupal 8 CM YAMLFilenames In Drupal 8 • < module >.< component >.yml system.settings.yml 
 views.settings.yml • < module >.< component >.< entity >.yml image.style.medium.yml 
 views.view.content.yml
  • 37.
  • 38.
    Configuration API Configuration Schema •Needed to define what your configuration will hold • Used to define what types of data the configuration will contain • See more at: https://drupal.org/node/1905070
  • 39.
    Configuration API system.schema.yml system.site: type: mapping label:'Site information' mapping: uuid: type: string label: 'Site UUID' name: type: label label: 'Site name' mail: type: email label: 'E-mail address' slogan: type: label label: 'Slogan' page: type: mapping label: 'Pages' mapping: 403: type: path label: 'Default 403 (access denied) page' 404: type: path label: 'Default 404 (not found) page' front: type: path label: 'Default front page' admin_compact_mode: type: boolean label: 'Compact mode' weight_select_max: type: integer label: 'Weight element maximum value' langcode: type: string label: 'Default language'
  • 40.
    Configuration API system.schema.yml Schema name
 - Used to reference this configuration 
 - Also shows the configuration filename “system.site.yml” system.site: type: mapping label: 'Site information' mapping: uuid: type: string label: 'Site UUID' name: type: label label: 'Site name'
  • 41.
    Configuration API Container datatype - ‘mapping’ is for key value sets - allows for associative arrays of 
 different data types system.site: type: mapping label: 'Site information' mapping: uuid: type: string label: 'Site UUID' name: type: label label: 'Site name' system.schema.yml
  • 42.
    Configuration API system.site: type: mapping label:'Site information' mapping: uuid: type: string label: 'Site UUID' name: type: label label: 'Site name' Label - Used as an interface label system.schema.yml
  • 43.
    Configuration API system.site: type: mapping label:'Site information' mapping: uuid: type: string label: 'Site UUID' name: type: label label: 'Site name' system.schema.yml Start of mapping section Item key Item type Item label
  • 44.
    Configuration API 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
  • 45.
    Configuration API Getting Configuration uuid:'' name: Drupal mail: '' slogan: '' page: 403: '' 404: '' front: user admin_compact_mode: false weight_select_max: 100 langcode: en $config = Drupal::config('system.site'); $email = $config->get('mail'); $cofig = Drupal::config(‘system.site') $page403 = $config->get('page.403'); 

  • 46.
    Configuration API Setting Configuration $config= Drupal::config('system.site'); $config->set(‘mail’, ‘test@example.com’); $config->save(); 
 
 $config = Drupal::config(‘system.site’)->set('mail', ‘test@example.com’); 
 $config->save();
 
 
 
 Drupal::config(‘system.site’)->set('mail', ‘test@example.com)- >save();
  • 47.
    Configuration API Clear Configuration $config= Drupal::config('system.site'); $config->clear('mail')->save(); 
 
 Drupal::config('system.site')->delete();
  • 48.
    Configuration API 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
  • 49.
    Configuration API Contact CategoryInterface namespace Drupalcontact;
 
 use DrupalCoreConfigEntityConfigEntityInterface; 
 /** * Provides an interface defining a contact category entity. */ interface CategoryInterface extends ConfigEntityInterface { 
 }
  • 50.
  • 51.
    Configuration API class Categoryextends ConfigEntityBase implements CategoryInterface { /** * The category ID. * * @var string */ public $id; /** * The category label. * * @var string */ public $label; /** * List of recipient e-mail addresses. * * @var array */ public $recipients = array(); /** * An auto-reply message to send to the message author. * * @var string */ public $reply = ''; /** * Weight of this category (used for sorting). * * @var int */ public $weight = 0;
  • 52.
    Configuration API contact.category.personal.yml id: personal
 label:'Personal contact form'
 recipients: { }
 reply: ''
 weight: 0
 status: true
 uuid: 43155e41-8a58-4264-ab00-be97a0736aa0 langcode: en
 dependencies: { } $contact_category = $this->entityManager() ->getStorage('contact_category') ->load(‘personal'); $contact_category->label();
  • 53.
    Configuration API contact.category.personal.yml id: personal
 label:'Personal contact form'
 recipients: { }
 reply: ''
 weight: 0
 status: true
 uuid: 43155e41-8a58-4264-ab00-be97a0736aa0 langcode: en
 dependencies: { } $config = Drupal::config('contact.category.personal')->get(); $label = $config['label'];
 
 $label = Drupal::config(‘contact.category.personal')->get('label');
  • 54.
    Configuration API Drush Export configfrom the active configuration to the staging directory drush config-export drush cex
  • 55.
    Configuration API Drush Import thestaging configuration into the active configuration drush config-import drush cim
  • 56.
    Configuration API Workflow • Stagingconfig 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
  • 57.
  • 58.
  • 59.
  • 60.
    Configuration API Drupal 7 •Configuration management has been back ported • Configuration Management module 
 https://drupal.org/project/configuration
  • 61.
  • 62.
    Configuration API Resources • CreatingDrupal 8.x modules 
 https://drupal.org/developing/modules/8 • Configuration API in Drupal 8 
 https://drupal.org/node/1667894 • Understanding Drupal 8's config entities 
 http://www.previousnext.com.au/blog/understanding-drupal-8s-config-entities • Configuration schema/metadata 
 https://drupal.org/node/1905070 • Configuration inspector for Drupal 8 
 https://drupal.org/project/config_inspector
  • 63.
  • 64.