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. 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
  2. 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
  3. <?php 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'); ... } }
  4. <?php // web/sites/default/settings.php ... /** * Change the config export

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

    directory to * "/var/www/config/default". */ $config_directories['sync'] = DRUPAL_ROOT . '/../config/default';
  6. <?php // 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';
  7. <?php // 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;
  8. CONFIGURATION SPLIT CONFIGURATION SPLIT Different configuration directories One per 'split'

    Configuration per environment Active split set in settings.php
  9. <?php // web/sites/default/settings.local.php ... /** * Enable the live split.

    */ $config['config_split.config_split.live']['status'] = TRUE;
  10. # 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.'
  11. // 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; } ... }
  12. // 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); } }
  13. // 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; } ...
  14. 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
  15. 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