Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Configuring all the Things (in Drupal 8)

Configuring all the Things (in Drupal 8)

Oliver Davies

July 25, 2018
Tweet

More Decks by Oliver Davies

Other Decks in Programming

Transcript

  1. CONFIGURING ALL
    CONFIGURING ALL
    THE THINGS!
    THE THINGS!
    By Oliver Davies (@opdavies)

    View Slide

  2. CONFIGURATION MANAGEMENT
    CONFIGURATION MANAGEMENT
    DRUPAL 8
    DRUPAL 8

    View Slide

  3. Web Developer, System Administrator
    Senior Developer at Microserve
    Part-time freelancer
    Acquia certified Drupal 8 Grand Master
    Drupal 7 and 8 core contributor
    @opdavies
    www.oliverdavies.uk

    View Slide

  4. WHAT IS
    WHAT IS
    CONFIGURATION?
    CONFIGURATION?

    View Slide

  5. WHAT IS CONFIGURATION?
    WHAT IS CONFIGURATION?
    Site settings (site name, front page path)
    Content types
    URL alias patterns
    API keys
    Date formats
    Which modules are enabled
    Block placements
    Field configuration and storage
    Paragraphs
    Image styles
    Taxonomy vocabularies
    User roles and permissions
    Views

    View Slide

  6. THE CONFIGURATION MANAGEMENT
    THE CONFIGURATION MANAGEMENT
    INITIATIVE
    INITIATIVE

    View Slide

  7. View Slide

  8. FEATURES
    FEATURES IN CORE
    IN CORE

    View Slide

  9. FEATURES
    FEATURES IN CORE
    IN CORE

    View Slide

  10. USING
    USING
    CONFIGURATION
    CONFIGURATION

    View Slide

  11. // Drupal 7
    variable_set('mymodule_setting', $value);
    variable_get('mymodule_setting');

    View Slide

  12. // Drupal 8
    $config = \Drupal::config('site.settings');
    $config->get('page.front');

    View Slide

  13. class ExampleController {
    private $config;
    public function __construct(
    ConfigFactoryInterface $config_factory
    ) {
    $this->config = $config_factory->get('system.site');
    }
    public function index() {
    $result = $this->config->get('page.front');
    ...
    }
    }

    View Slide

  14. IMPORTING AND EXPORTING
    IMPORTING AND EXPORTING
    CONFIGURATION
    CONFIGURATION

    View Slide

  15. View Slide

  16. View Slide

  17. View Slide

  18. View Slide

  19. $ drush config-export
    $ drush config-import

    View Slide

  20. $ drush config:export
    $ drush config:import

    View Slide

  21. $ drupal config:export
    $ drupal config:export:single
    $ drupal config:import

    View Slide

  22. // web/sites/default/settings.php
    ...
    /**
    * Change the config export directory to
    * "/var/www/config/default".
    */
    $config_directories[CONFIG_SYNC_DIRECTORY] =
    DRUPAL_ROOT . '/../config/default';

    View Slide

  23. // web/sites/default/settings.php
    ...
    /**
    * Change the config export directory to
    * "/var/www/config/default".
    */
    $config_directories['sync'] =
    DRUPAL_ROOT . '/../config/default';

    View Slide

  24. DEMO
    DEMO

    View Slide

  25. OVERRIDING
    OVERRIDING
    CONFIGURATION
    CONFIGURATION

    View Slide

  26. // web/sites/default/default.settings.php
    ...
    /**
    * Configuration overrides.
    */
    # $config['system.file']['path']['temporary'] = '/tmp';
    # $config['system.site']['name'] = 'My Drupal site';
    # $config['system.theme']['default'] = 'stark';
    # $config['user.settings']['anonymous'] = 'Visitor';

    View Slide

  27. // web/sites/example.settings.local.php
    ...
    /**
    * Show all error messages, with backtrace information.
    */
    $config['system.logging']['error_level'] = 'verbose';
    /**
    * Disable CSS and JS aggregation.
    */
    $config['system.performance']['css']['preprocess'] = FALSE;
    $config['system.performance']['js']['preprocess'] = FALSE;

    View Slide

  28. SPLITTING
    SPLITTING
    CONFIGURATION
    CONFIGURATION

    View Slide

  29. DGO.TO/
    DGO.TO/CONFIG_SPLIT
    CONFIG_SPLIT

    View Slide

  30. CONFIGURATION SPLIT
    CONFIGURATION SPLIT
    Different configuration directories
    One per 'split'
    Configuration per environment
    Active split set in settings.php

    View Slide

  31. View Slide

  32. View Slide

  33. // web/sites/default/settings.local.php
    ...
    /**
    * Enable the live split.
    */
    $config['config_split.config_split.live']['status'] = TRUE;

    View Slide

  34. IGNORING
    IGNORING
    CONFIGURATION
    CONFIGURATION

    View Slide

  35. DGO.TO/
    DGO.TO/CONFIG_IGNORE
    CONFIG_IGNORE

    View Slide

  36. View Slide

  37. ADDING CONFIGURATION TO YOUR
    ADDING CONFIGURATION TO YOUR
    CUSTOM MODULES
    CUSTOM MODULES

    View Slide

  38. # config/install/override_node_options.settings.yml
    general_permissions: true
    specific_permissions: true

    View Slide

  39. # config/schema/override_node_options.schema.yml
    override_node_options.settings:
    type: config_object
    label: 'Override Node Options settings'
    mapping:
    general_permissions:
    type: boolean
    label: 'Use general permissions.'
    specific_permissions:
    type: boolean
    label: 'Use content type specific permissions.'

    View Slide

  40. // src/Form/SettingsForm.php
    class SettingsForm extends ConfigFormBase {
    ...
    public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this->config('override_node_options.settings');
    $form['general_permissions'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('General permissions, across all node types'),
    '#default_value' => $config->get('general_permissions'),
    ];
    $form['specific_permissions'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('Specific permissions, for each individual node type'),
    '#default_value' => $config->get('specific_permissions'),
    ];
    return $form;
    }
    ...
    }

    View Slide

  41. // src/Form/SettingsForm.php
    class SettingsForm extends ConfigFormBase {
    ...
    public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this->config('override_node_options.settings')
    ->set('general_permissions', $form_state->getValue('general_permissions'))
    ->set('specific_permissions', $form_state->getValue('specific_permissions'))
    ->save(TRUE);
    }
    }

    View Slide

  42. // src/NodePermissions.php
    class NodePermissions {
    ...
    public function nodeTypePermissions() {
    $permissions = [];
    $config = \Drupal::config('override_node_options.settings');
    if ($config->get('general_permissions')) {
    $this->addGeneralPermissions($permissions);
    }
    if ($config->get('specific_permissions')) {
    $this->addSpecificPermissions($permissions);
    }
    return $permissions;
    }
    ...

    View Slide

  43. INSTALLING FROM AN EXISTING
    INSTALLING FROM AN EXISTING
    CONFIGURATION
    CONFIGURATION

    View Slide

  44. DGO.TO/
    DGO.TO/CONFIG_INSTALLER
    CONFIG_INSTALLER

    View Slide

  45. The Configuration Installer project provides a means to install
    a Drupal 8 site using existing configuration.
    This is not a module, Configuration Installer is an installation
    profile that loads configuration from a specified folder to
    'install' the site. This is not a real installation profile either, it
    helps to install a real installation profile (e.g. minimal) along
    with an existing set of configuration

    View Slide

  46. CMI
    CMI 2.0
    2.0

    View Slide

  47. Drupal 8's built-in config system is a HUGE step forward.
    However, now that the community has a couple of years of
    building Drupal 8 sites behind them, various limitations have
    surfaced: various common workflows are not natively
    supported; core's config APIs have missing functionality.
    While many of these problems have contrib workarounds,
    often these solutions can conflict with one another, and
    there's no one set of best practices that works for all

    View Slide

  48. DGO.TO/
    DGO.TO/CMI2
    CMI2

    View Slide

  49. QUESTIONS?
    QUESTIONS?

    View Slide

  50. THANKS!
    THANKS!
    @OPDAVIES
    @OPDAVIES
    WWW.OLIVERDAVIES.UK
    WWW.OLIVERDAVIES.UK

    View Slide