Hello, We have till now covered:

  • Created a custom module 'hello_world'.
  • Made a page using `routing.yml` file.
  • Made a custom form with fields First name and Last name.


Next Steps: In this post we will make a default configuration that would bring out default name in our form fields and change these default configurations.
We will also cover how you can save configurations in files folder and put these configurations under version control.
To make default configuration, we are going to make a new folder config under our hello_world module. In this I will make install folder and in this folder I will make a file hello_world.settings.yml.

  • Set the variables first_name and last_name in the `hello_world.settings.yml`
    hello:
      first_name: 'Nitish'
      last_name: 'Guleria'
  • To use this set variable in the we are going to use `config()` method
    <?php
    $config = \Drupal::config('hello_world.settings');
    $first_name = $config->get('first_name');
  • After placing these default configurations in settings.yml file disable and re-enable the module.
  • Now in our `HelloWorldForms` file put these variables in `#default_value` of these fields.
    <?php
    $form['candidate_name']['first_name'] = array(
    '#type' => 'textfield',
    '#title' => t('First Name:'),
    '#required' => TRUE,
    '#default_value' =>
    \Drupal::config('hello_world.settings')->get('hello.first_name'),
    );
    $form['candidate_name']['last_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Last Name:'),
    '#required' => TRUE,
    '#default_value' =>
    \Drupal::config('hello_world.settings')->get('hello.last_name'),

This way you can get default values in your form. In order to set values I will put

<?php
\Drupal::config('hello_world.settings')
      ->set('mysettingname', 'myfirstvariable')
      ->save();

where mysettingname would be hello.first_name or hello.last_name and myfirstvariable would be the form state value.

To Summerize: In drupal 8 we have not got variable_get() and variable_set() functions, instead we set variables in the install file and get and set variables through \Drupal::config().

Cheers.


Edit: 28th september 2017-09
We can also define our variables in .schema.yml file which would be placed in our custom module where name of module is nitish_guleria. nitish_guleria->config->schema->nitish_guleria.schema.yml

nitish_guleria.settings:
  type: mapping
  label: Variable Settings of My Module
  mapping:
    custom_block_settings:
      type: string
    custom_block_setting2:
      type: string

here custom_block_settings and custom_block_settings2 are the custom variables