Drupal 8 module development #4 – creating a settings file
- Drupal 8 module development #1 – kicking off
- Drupal 8 module development #2 – adding basic routing
- Drupal 8 module development #3 – adding a settings page
- Drupal 8 module development #3 – adding a settings page – revision
- Drupal 8 module development #4 – creating a settings file
This is the 4th of several on-going blog post series which aim to educate on the process of porting modules to Drupal 8 with real life examples by porting a popular Drupal 7 module to Drupal 8.
In previous articles we have started with Drupal 8 initial module porting and worked our way about adding a route and implementing the settings form controller so that we can present an administrative UI for managing the various configuration options which the module exposes.
Next up will be to create and define those configurations options. In Drupal 7 we used the variable system to save and load configuration options, thus calling variable_set() and variable_get() in our modules, and all of this configuration data was saved in the database just like the rest of Drupal’s data storage. This brought clutter and quite some mess, where pure data is mixed with configuration information and such. Out of the necessity to deal with this in Drupal 7 we’ve turned into modules like Features, Strongarm and others, although none of these is an ideal solution. For this reason, Drupal 8 defined the configuration initiative which aim is to attack this problem and make this simpler.
For a module to declare settings, it needs to create a config/ directory in the module’s root directory and create a configuration file <modulename>.settings.yml. For example the globalredirect module has the following settings declared in config/globalredirect.settings.yml with their default options too:
deslash: '1' nonclean_to_clean: '1' trailing_zero: '0' menu_check: '0' case_sensitive_urls: '1' language_redirect: '0' canonical: '0' content_location_header: '0' term_path_handler: '1' frontpage_redirect: '1' ignore_admin_path: '1'
While it’s not a requirement, it is advised to also create the schema settings file which extends the exported settings with more metadata and allows for localization amongst other things. To make this happen, we will create the directory config/schema/ and inside it include the file globalredirect.schema.yml with the following content which shows a couple of configuration settings only to not spam you with code:
globalredirect.settings: type: mapping label: 'Global Redirect Settings' mapping: deslash: type: boolean label: 'Deslash' nonclean_to_clean: type: boolean label: 'Non-clean to Clean'
Using module configuration
We’ve seen previously how to make use of a module’s declared settings but we’ll revisit it shortly now.
To interact with the exposed settings of the module, we can make use of the configFactory and it’s setters and getters. For example, to read the module’s settings we can do:
$config = $this->config('globalredirect.settings'); $deslash = $config->get('deslash');