Clicky

Configuration in Drupal 8


There are some changes in the system for storing configuration data in Drupal 8. The new system facilitates the import and export of the configuration. The default configuration is supplied with modules, themes and stored in YAML files (for example: image.settings.yml), and when the module or theme is enabled, it is imported into the active configuration repository. By default, the active configuration is stored in the database. The Drupal 8 kernel includes the Configuration Manager module.

Configuration Manager provides the following key features: update management, deployment of software and operating systems, integration with NAP, hardware and software inventory, remote management, Windows virtualized and mobile systems management. Configuration Manager provides the user interface for importing and exporting configuration changes. Configuration Manager allows you to deploy the configuration from one environment to another - provided that it is a single site. The site is identified using a Universal Unique Identifier (UUID). This module eliminates the need for such additional modules as Strongarm and Features.

In Drupal 7 for content type transfer you had to download Features, make settings, but now you only need to paste the copy configuration - and you will see the required type of content. You don’t need to make changes right away to a live site. The purpose of the configuration system in Drupal 8 is to give you the opportunity to do all kinds of customizations on the test site, and to live up to the ready settings.

The Configuration Manager page is located in Manage> Configuration> Development> Configuration management or admin/config/development / configuration.

Configuration Manager allows:

        • synchronize configuration;
        • perform single import / export;
        • complete import / export.
        For the single export from the test to the live site, you need to do the following:
        1.  Select the "Export" tab;	
        2. Select the configuration type;
        3. Select the desired configuration;
        4. Copy the configuration text 
        
        To import a pre-exported configuration individually, you must do the following:
        
        1. Select the "Import" tab;
        2. Select the configuration type;
        3. Insert the text of the configuration;
        4. Press the Import button.
        

To fully export the configuration, select the "Full Import/Export" tab, then the "Export" tab. Then you just need to click on the Export button. After these steps, the browser will begin downloading the configuration archive.

To import a newly downloaded configuration, follow these steps:
Select: "Full Import / Export" tab → "Export" tab → the archive file and → click on the button Upload.
By default, Drupal 8 stores configuration files in folders:

• 'sites/defaul /files/config/active'

• 'site /default/files/config/staging'

You can change the configuring location in the settings.php file. For this you need to:
$ config_directories ['active'] = 'your_path' and $ config_directories ['active'] = 'your_path' specify the required path.
Drupal 8 provides us with a powerful API for code configuration. In the next step, you can see how to create, modify, and delete settings. Now let's look at the configuration work on an example of our own module.

In Drupal 7, one of the most common ways to save settings was to use variable_get () and variable_set (). In Drupal 8, they were replaced with a new configuration system. Drupal 8 has removed function variable_get () / variable_set (). You need to work with the settings as follows:

    
        $config = \Drupal :: configFactory () -> getEditable ($ key) - we get the required configuration.
        $configuration-> set ($key, $value) - set the required value.
        $configuration-> save () - save the changes.
        

The default module settings can be stored in the file your_module_name.settings.yml, which must be placed in the following folder:
config/install
To set a default dynamic value that can`t be written in your_module_name.settings.yml file, you need to use hook_install ().

Example:
 
        <? php
        / **
        * Implements hook_install ().
        * /
        function idevels_config_example_install () {
        // Set default values for config which require dynamic values.
        \Drupal :: configFactory () -> getEditable ('idevels_config_example.settings') 
        -> set ('some_data2', \ Drupal :: configFactory () -> getEditable ('system.site')
        -> get ('name') )
        -> save ();
        }
        ?>  

This code sets the name of the site as the value of the field some_data2. Let's look at this example in more detail. Using the line we get the object with the necessary settings, in the line we set the name of the site in the field some_data2 and in the end we need to save our settings.
Drupal 7 used the system_settings_form () function to create the module configuration form. In Drupal 8, the form with the settings is created as follows:

 
        <? php
 
         / **
         * @file
         * Contains \ Drupal \ Demo \ Form \ DemoForm.
         * /

        namespace drupal \ idevels_config_example \ form;

        use Drupal \ Core \ Form \ ConfigFormBase;
        use Drupal \ Core \ Form \ FormStateInterface;
        use Drupal \ Core \ Config \\ConfigFactory;

        class TestConfForm extends ConfigFormBase {
          public function getFormId() {
            return 'example_set_form';
          }

          /**
           * @param array $form
           * @param FormStateInterface $form_state
           * @return array
           */
          public function buildForm(array $form, FormStateInterface $form_state) {
            $config = $this->config('idevels_config_example.settings');
            $form['custom_field'] = [
              '#type' => 'textfield',
              '#title' => $this->t('Some text data'),
              '#default_value' => $config->get('some_data2'),
            ];
            $form['custom_field2'] = [
              '#type' => 'textfield',
              '#title' => $this->t('Some text data'),
              '#default_value' => $config->get('some_data'),
            ];

            return parent::buildForm($form, $form_state);
          }

          public function submitForm(array &$form, FormStateInterface $form_state) {
            $config = \Drupal::configFactory()->getEditable('idevels_config_example.settings')
              ->set('some_data2', $form_state->getValue('custom_field'))
              ->set('some_data', $form_state->getValue('custom_field2'))
              ->save();

            parent::submitForm($form, $form_state);
          }

          /**
           * Gets the configuration names that will be editable.
           *
           * @return array
           *   An array of configuration object names that are editable if called in
           *   conjunction with the trait's config() method.
           */
          protected function getEditableConfigNames()
          {
            return ['idevels_config_example.settings'];
          }
        }
    

It is worthwhile to note that the imitation of the class of your form must be done from the class ConfigFormBase, not the FormBase, as in the usual forms.
This looks like a created form:



It should be noted that in the second field, the default value is the data from the configuration file idevels_config_example.settings.yml, and the first one is the site name that we get in hook_install ().
Here's what the file idea idevels_config_example.settings.yml looks like:
some_data: 'default data' To create your own content type in the module, you need to create the required YAML files in the folder your_module/config/install/. For example, to create a content type "my_content_type" that will have one body field, you need to create the following files:

        core.entity_form_display.node.my_content_type.default.yml:
        uuid: 0e54b601-1c72-4989-8900-b34fc31c7721
        langcode: en
        status: true
        dependencies:
          config:
            - field.field.node.my_content_type.body
            - node.type.my_content_type
          module:
            - entity_reference
            - path
            - text
        id: node.my_content_type.default
        targetEntityType: node
        bundle: my_content_type
        mode: default
        content:
          title:
            type: string_textfield
            weight: 0
            settings:
              size: 60
              placeholder: ''
            third_party_settings: {}
          uid:
            type: entity_reference_autocomplete
            weight: 1
            settings:
              match_operator: CONTAINS
              size: 60
              placeholder: ''
            third_party_settings: {}
          created:
            type: datetime_timestamp
            weight: 2
            settings: {}
            third_party_settings: {}
          promote:
            type: boolean_checkbox
            weight: 3
            settings:
              display_label: true
            third_party_settings: {}
          sticky:
            type: boolean_checkbox
            weight: 4
            settings:
              display_label: true
            third_party_settings: {}
          path:
            type: path
            weight: 5
            settings: {}
            third_party_settings: {}
          body:
            type: text_textarea_with_summary
            weight: 6
            settings:
              rows: 9
              summary_rows: 3
              placeholder: ''
            third_party_settings: {}
        hidden: {}
        third_party_settings: {}
        field.field.node.my_content_type.body.yml:
        uuid: d0bf6e80-a1e7-4139-b446-70338272f3c9
        langcode: en
        status: true
        dependencies:
          config:
            - field.storage.node.body
            - node.type.my_content_type
          module:
            - text
        id: node.my_content_type.body
        field_name: body
        entity_type: node
        bundle: my_content_type
        label: FieldBody
        description: ''
        required: false
        translatable: true
        default_value: {}
        default_value_callback: ''
        settings:
          display_summary: true
        third_party_settings: {}
        field_type: text_with_summary
        node.type.my_content_type.yml:
        uuid: f07bfea6-da48-49c9-bb32-500f4ecc1c37
        langcode: en
        status: true
        dependencies: {}
        name: 'My Content Type'
        type: my_content_type
        description: 'custom content type.'
        help: ''
        new_revision: false
        preview_mode: 1
        display_submitted: true
        third_party_settings: {}

Now, when installing the module, a new type of content will be created, the settings of which are described in the above files. Changes in the configuration of the newly created content type can be migrated between sites using the Configuration Management module, the work described above.

I think that Drupal 8 has a very user-friendly configuration system, which makes it not necessary to use third-party modules.

Quick Free Quote
We respect your privacy. NO SPAM No selling your personal data.
We will respond to your query & collect further details within 24 hours. Guaranteed!

Support

We are friendly people who love to talk. So go ahead and contact us.

Technologies

Awards & Certification