Third & GroveThird & Grove
Feb 19, 2016 - Curtis Ogle

Creating a Simple Configuration Form in Drupal 8

Drupal 8’s way of managing configuration forms is fairly different from Drupal 7. In this post, we will create a simple module that has a configuration form with a few fields.

First, start off by creating a module skeleton. You could use Drupal Console for this, or just by following the tutorial found here.

The first thing I recommend you doing is creating the route for your configuration form. For this example, I’m calling the module Simple, so I will be adding simple.routing.yml. Create your mymodule.routing.yml file and place it inside of your module directory. Place this code in there:

 

simple.config:

  path: '/admin/config/simple/config'

  defaults:

    _form: '\Drupal\simple\Form\SimpleConfigForm'

    _title: 'Simple Configuration'

  requirements:

    _permission: 'administer site configuration'

 

Next, in mymodule/src/Form/ create a new file called SimpleConfigForm.php. Paste the following code in there:

 

<?php
 
/**
 
 * @file
 
 * Contains \Drupal\simple\Form\SimpleConfigForm.
 
 */
 
namespace Drupal\simple\Form;
 
use Drupal\Core\Form\ConfigFormBase;
 
use Drupal\Core\Form\FormStateInterface;
 
class SimpleConfigForm extends ConfigFormBase {
 
  /**
 
   * {@inheritdoc}
 
   */
 
  public function getFormId() {
 
    return 'simple_config_form';
 
  }
 
  /**
 
   * {@inheritdoc}
 
   */
 
  public function buildForm(array $form, FormStateInterface $form_state) {
 
    $form = parent::buildForm($form, $form_state);
 
    $config = $this->config('simple.settings');
 
    $form['email'] = array(
 
      '#type' => 'textfield',
 
      '#title' => $this->t('Email'),
 
      '#default_value' => $config->get('simple.email'),
 
      '#required' => TRUE,
 
    );
 
    $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('Node Types'),
 
      '#options' => $node_type_titles,
 
      '#default_value' => $config->get('simple.node_types'),
 
    );
 
    return $form;
 
  }
 
  /**
 
   * {@inheritdoc}
 
   */
 
  public function submitForm(array &$form, FormStateInterface $form_state) {
 
    $config = $this->config('simple.settings');
 
    $config->set('simple.email', $form_state->getValue('email'));
 
    $config->set('simple.node_types', $form_state->getValue('node_types'));
 
    $config->save();
 
    return parent::submitForm($form, $form_state);
 
  }
 
  /**
 
   * {@inheritdoc}
 
   */
 
  protected function getEditableConfigNames() {
 
    return [
 
      'simple.settings',
 
    ];
 
  }
 
}

 

Now, I obviously brushed over most of this code, but I will give a quick overview of what it does. First, we define the form id in the getFormId() function. You could use this id in a hook_alter method to modify things. Next the buildForm() method creates a form with two items. The first is a simple text field that is required. It is labeled to accept an email address. The second form item is a group of checkboxes.

 

We use the \Drupal\node\Entity\NodeType::loadMultiple() method to load a list of all node types and we create a checkbox for each.

 

Notice that on both form fields, we have a #default_value. This is used to load the config of a previously saves form state. So, after you enter in fields, the page will refresh and your new values will be stored. We populate the field items for the next time you visit the form.

Lastly, in the submitForm function, we simply load up our configuration and save the values that are found in the form state. From there we pass the flow to the parent object so it can flow through the stack.

That’s it! All you have to do is go and enable the module and you should be able to access the form by going to /admin/config/simple/config.

Happy Drupaling!