[Drupal-8] How to create configuration form in Drupal 8

August 24, 2016 - 18:44

Drupal 8’s way of managing configuration forms is entirely different from Drupal 7. Assuming that you have a custom module, we shall see how to create simple configuration form.

First step is to create a route for the configuration form. Place the below code in 'your_module.routing.yml' file.

your_module.config:
    path: '/admin/config/your_module/settings'
    defaults:
      _form: '\Drupal\your_module\Form\ModuleConfigForm'
      _title: 'Module Configurations'
    requirements:
      _permission: 'administer site configuration'

Next step is to create a new file called 'ModuleConfigForm.php' in your_module/src/Form/. Paste the following code in the created file:

     /**    
     * @file     
     * Contains \Drupal\simple\Form\ModuleConfigForm.     
     */     
    namespace Drupal\your_module\Form;
     
    use Drupal\Core\Form\ConfigFormBase;
     
    use Drupal\Core\Form\FormStateInterface;
     
    class ModuleConfigForm extends ConfigFormBase {
      /**
       * {@inheritdoc}
       */
      public function getFormId() {
        return 'your_module_config_form';
      }

      /**
       * {@inheritdoc}
       */
      public function buildForm(array $form, FormStateInterface $form_state) {
        $form = parent::buildForm($form, $form_state);
        $config = $this->config('your_module.settings');
        $form['example_value1'] = array(
          '#type' => 'textfield',
          '#title' => $this->t('Example Value 1'),
          '#default_value' => $config->get('your_module.example_value1'),
          '#required' => TRUE,
        );
        $form['example_value2'] = array(
          '#type' => 'textfield',
          '#title' => $this->t('Example Value 2'),
          '#default_value' => $config->get('your_module.example_value2'),
        );
        $node_types = \Drupal\node\Entity\NodeType::loadMultiple();
        $node_type_titles = array();
        foreach ($node_types as $machine_name => $val) {
          $node_type_titles[$machine_name] = $val->label();
        }
        $form['node_types'] = array(
          '#type' => 'checkboxes',
          '#title' => $this->t('For Node Types'),
          '#options' => $node_type_titles,
          '#default_value' => $config->get('your_module.node_types'),
        );
        return $form;
      }

      /**
       * {@inheritdoc}
       */
      public function submitForm(array &$form, FormStateInterface $form_state) {
        $config = $this->config('your_module.settings');
        $config->set('your_module.example_value1', $form_state->getValue('example_value1'));
        $config->set('your_module.example_value2', $form_state->getValue('example_value2'));
        $config->set('your_module.node_types', $form_state->getValue('node_types'));
        $config->save();

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

      /**
       * {@inheritdoc}
       */
      protected function getEditableConfigNames() {
        return [
          'your_module.settings',
        ];
      }
    }

That’s it! Now you could access the form by going to /admin/config/your_module/settings. For getting the values in your module use the following lines of code.

$config = \Drupal::config('your_module.settings');
    // This will print the value of example_value1.
    print $config->get('your_module.example_value1');

Hope this helps. Please feel free to get in touch with us if any queries