Session given at DrupalCamp Baltics 2013. Overview of the configuration management system in Drupal 8. Covers the api, config entities, context system.
2. who am I?
Kristof De Jaeger
@swentel
Co-maintainer of Field API
Lead maintainer of Display Suite
Developer @ Wunderkraut
Friday 23 August 13
3. Outline
• What’s the problem
• How did we solve it
• Simple static settings
• Configuration entities
• Deployment - with demo
• Configuration schema
• Context, events and overrides
Friday 23 August 13
4. What problems are we
trying to solve?
• Variable soup
Live
Save
textSetting 1
Setting 2 label
Database Database
Dev
TEST
test test test test
test test test test
test test test test
test test
node/4admin/config/foo
Welcome
This is real
content on the
live site that end
users are viewing
node/4
Save
old textSetting 1
Setting 2 label
admin/config/foo
Friday 23 August 13
5. Live
Save
textSetting 1
Setting 2 label
Database Database
Dev
TEST
test test test test
test test test test
test test test test
test test
node/4admin/config/foo
Welcome
This is real
content on the
live site that end
users are viewing
node/4
Save
old textSetting 1
Setting 2 label
admin/config/foo
Danger!
Want to bring over configuration
changes from dev, but not
overwrite live content!
What problems are we
trying to solve?
Friday 23 August 13
6. What problems are we
trying to solve?
variable_set()/variable_get()
ctools_export_object()/
ctools_export_load_object()
db_select()/db_update()/
db_delete()
$conf[...];
hook_update_N()
drush fu
http://www.flickr.com/photos/bean/322616749
napkins
Friday 23 August 13
7. The solution
• Files using theYAML specification
• Active and staging directory
• Cached in the database using a standard
cache interface
• Config directory changed via settings.php
Friday 23 August 13
23. • system_settings_form is dead
• add your own submit callback
• you are responsible for saving configuration
• ship with default configuration file
simple settings
Friday 23 August 13
31. */
class Category extends ConfigEntityBase {
/**
* The category ID.
*/
public $id;
/**
* The category UUID.
*/
public $uuid;
/**
* The category label.
*/
public $label;
/**
* List of recipient e-mail addresses.
*/
public $recipients = array();
/**
* An auto-reply message to send to the message author.
*/
public $reply = '';
/**
* Weight of this category (used for sorting).
*/
public $weight = 0;
}
Friday 23 August 13
50. Basic data types for configuration
undefined:
label: 'Undefined'
class: 'DrupalCoreConfigSchemaProperty'
mapping:
label: Mapping
class: 'DrupalCoreConfigSchemaMapping'
sequence:
label: Sequence
class: 'DrupalCoreConfigSchemaSequence'
Friday 23 August 13
51. Simple extended data types
# Human readable string that must be plain text and editable
with a text field.
label:
type: string
label: 'Label'
translatable: true
# Internal Drupal path
path:
type: string
label: 'Path'
# Human readable string that can contain multiple lines of text
or HTML.
text:
type: string
label: 'Text'
translatable: true
Friday 23 August 13
52. Complex extended data type
# Mail text with subject and body parts.
mail:
type: mapping
label: "Mail"
mapping:
"subject":
type: text
label: "Subject"
"body":
type: text
label: "Body"
Friday 23 August 13
56. class ConfigGlobalOverrideSubscriber implements
EventSubscriberInterface {
static function getSubscribedEvents() {
$events['config.init'][] = array('configInit',
30);
return $events;
}
public function configInit(ConfigEvent $event) {
global $conf;
$config = $event->getConfig();
if (isset($conf[$config->getName()])) {
$config->setOverride($conf[$config->getName()]);
}
}
}
Global overrides
Friday 23 August 13
57. Break out of contexts
// Enter the override-free context, so we can ensure no
overrides are applied.
config_context_enter('config.context.free');
// Get system site maintenance message text from the original
config.
$message = config('system.maintenance')->get('message');
// Leave the override-free context.
config_context_leave();
Friday 23 August 13
58. Get into contexts
// Enter a user specific context.
$context = config_context_enter("DrupaluserUserConfigContext");
// Set the account to use on the context.
$context->setAccount($account);
$mail_config = Drupal::config('user.mail');
// Do stuff...
config_context_leave();
Friday 23 August 13
60. recap and advice
• key names/properties should have meaning
• Use config entities instead of tables
• Use getters/setters/methods on entities
• Include config schema (translation!)
• Upgrade functions available in update.inc
Friday 23 August 13
61. Please try it out!
• #drupal-cmi - Dedicated IRC channel
• docs - https://drupal.org/node/1667894
• help along - http://drupal.org/core-mentoring-hours
Friday 23 August 13