config management

64
Drupal 8: Configuration Management by Alex Goja

Upload: alexei-goja

Post on 24-Jan-2017

77 views

Category:

Internet


0 download

TRANSCRIPT

Page 1: Config management

Drupal 8: Configuration Management

by Alex Goja

Page 2: Config management

What Is Drupal Config? A quick quiz 01

Page 3: Config management

What is Drupal config ?

Blog post

Page 4: Config management

What is Drupal config ?

Permissions

Page 5: Config management

What is Drupal config ?

Vocabulary

Page 6: Config management

What is Drupal config ?

Taxonomy term

Page 7: Config management

What is Drupal config ?

Field definition

Page 8: Config management

What is Drupal config ?

Menu

Page 9: Config management

What is Drupal config ?

Variable

Page 10: Config management

What is Drupal config ?

Menu item

Page 11: Config management

What is Drupal config ?

User

Page 12: Config management

What is Drupal config ?

Contents of a block

Page 13: Config management

Content An article page, uploaded files

Session

State Configuration

Logged in status, shopping carts

Last cron run

Everything else

Page 14: Config management

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

Page 15: Config management

Current Situation02

Page 16: Config management

Current Situation

Drupal 7 Configuration How do we currently manage configuration in Drupal?

• Features

• Install/Update hooks

• Install Profiles

Page 17: Config management

Current Situation

Features

• Features ‘Overridden’ sadness

• Adding Features support for modules is not easy

• Export and import of certain configurations as modules

Page 18: Config management

Current Situation

Install/Update Hooks

• Create tables

• Set up variables

• Fill in ‘gaps’ in Features modules

Page 19: Config management

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

Page 20: Config management

Drupal 8 CM 03

Page 21: Config management

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

Page 22: Config management

Usage04

Page 23: Config management
Page 24: Config management
Page 25: Config management
Page 26: Config management
Page 27: Config management
Page 28: Config management
Page 29: Config management
Page 30: Config management
Page 31: Config management
Page 32: Config management
Page 33: Config management
Page 34: Config management

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

Page 35: Config management

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>/

Page 36: Config management

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

Page 37: Config management

Configuration API05

Page 38: Config management

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

Page 39: Config management

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'

Page 40: Config management

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'

Page 41: Config management

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

Page 42: Config management

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

Page 43: Config management

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

Page 44: Config management

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

Page 45: Config management

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');

Page 46: Config management

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();

Page 47: Config management

Configuration API

Clear Configuration $config = \Drupal::config('system.site'); $config->clear('mail')->save();

\Drupal::config('system.site')->delete();

Page 48: Config management

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

Page 49: Config management

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 { }

Page 50: Config management

Configuration API

Contact Category Entity

Page 51: Config management

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;

Page 52: Config management

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();

Page 53: Config management

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');

Page 54: Config management

Configuration API

Drush

Export config from the active configuration to the staging directory

drush config-export drush cex

Page 55: Config management

Configuration API

Drush

Import the staging configuration into the active configuration

drush config-import drush cim

Page 56: Config management

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

Page 57: Config management

Configuration API

Page 58: Config management

Configuration API

Page 59: Config management

Drupal 706

Page 60: Config management

Configuration API

Drupal 7

• Configuration management has been back ported

• Configuration Management module https://drupal.org/project/configuration

Page 61: Config management

Resources07

Page 62: Config management

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

Page 63: Config management

Questions ?

Page 64: Config management

Alex Goja

Team Lead

[email protected]