The primary mechanism for collecting input from users is Form, without them Drupal wouldn't be so much useful. This is also one of the first things Developer should learn when they start development using Drupal. Forms are fundamental to creating Drupal modules, whether asking someone to leave a comment or Administrator has option to turn the module configuration ON/OFF.

The configuration system / state system has replaced the variable system in D8. There is no variable table and no more variable_get() / variable_set() /  variable_del() calls. Configuration is stored in the database and synced with YML files on the disk for deployment purposes.

The $config object handles CRUD (Create/Read/Update/Delete) for YML files, so you simply use ::get(), ::set(), and ::save() methods and your data will be stored in the {module}.settings.yml file.

When we defined PageExampleForm, which is our new class. extend the FormBase class; simple implementation of the FormInterface which provides an interface for a form.

Drupal 8 provides an alternate class, ConfigFormBase, which enables additional functionality at the cost of increased complexity in the method. The primary benefit of this class is, improved interaction with the configuration system.

Lets Code:

The first thing you'll need to do is create a route for your form. In our example, it looks like this:

Step 1: In file, you define the configuration route:

    configure: your_module.admin_settings
ex: xai.settings.form

Step 2: In your_module.routing.yml file, you define the route:

The only difference between this route and one that displays non-form content on a page is the _form key instead of the usual _controller key. Here _form tells Drupal the location of the class that it should use when constructing our form object.

Note: We simply specify the class name here and not the method, like SettingsForm::buildForm. Because we've defined this route as a form, Drupal will call buildForm whenever someone requests /admin/config/system/xai.

          path: '/admin/config/your_module'
      _form: '\Drupal\your_module\Form\ModuleConfigurationForm'
      _title: 'your_module configuration screen'
     _permission: 'administer site configuration'

Step 3: Define tform  in your_module/src/Form/ModuleConfigurationForm.php :

SettingsForm extends ConfigFormBase

Also note that we've opted to extend the Drupal\Core\Form\ConfigFormBase class which provides some additional boilerplate code for system settings forms. There is a Drupal\Core\Form\FormBase class also.

Git Hib source code for configuration page

Once you have done withSettings.php form. you can access configuration page by visiting


Configuration page setting

Source Code:


comments powered by Disqus