config management
TRANSCRIPT
Drupal 8: Configuration Management
by Alex Goja
What Is Drupal Config? A quick quiz 01
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 Situation02
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
Usage04
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 API05
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 keyItem typeItem 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: Drupalmail: ''slogan: ''page: 403: '' 404: '' front: useradmin_compact_mode: falseweight_select_max: 100langcode: 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’, ‘[email protected]’); $config->save();
$config = \Drupal::config(‘system.site’)->set('mail', ‘[email protected]’); $config->save(); \Drupal::config(‘system.site’)->set('mail', ‘[email protected])- >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 Drupal\contact; use Drupal\Core\Config\Entity\ConfigEntityInterface; /**
* Provides an interface defining a contact category entity.*/
interface CategoryInterface extends ConfigEntityInterface { }
Configuration API
Contact Category Entity
Configuration APIclass 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 706
Configuration API
Drupal 7
• Configuration management has been back ported
• Configuration Management module https://drupal.org/project/configuration
Resources07
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 ?