Wednesday 13 March 2019

Create Configuration Form Programmatically Drupal 8

Create configurable form programmatically

To create a form, first we need a path. So create a path in .routing.yml file.
firstcustommodule.routing.yml
firstcustommodule.company_config_form:
  path: '/admin/config/companyconfig'
  defaults:
    _form: '\Drupal\firstcustommodule\Form\CompanyConfigForm'
    _title: 'Add Company Configurations'
  requirements:
    _permission: 'administer site configuration'

Now create CompanyConfigForm.php file in firstcustommodule > src > Form.
For creating configuration form, we need to extend ConfigFormBase class. When we extend this class, we have to define following methods.
public function getFormId(): return form id.
public function submitForm(): return the form.
protected function getEditableConfigNames(): return editable config settings.

All above mentioned methods are used in below code. 
<?php

/**
 * @file
 * Contains \Drupal\firstcustommodule\Form\CompanyConfigForm
 */

namespace Drupal\firstcustommodule\Form;

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

/**
 * Class for company config form.
 */
class CompanyConfigForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
   }

   /**
    * {@inheritdoc}
*/
  public function buildForm(array $form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
  }
}

Paste following code in the CompanyConfigForm.php file.

<?php

/**
 * @file
 * Contains \Drupal\firstcustommodule\Form\CompanyConfigForm
 */

namespace Drupal\firstcustommodule\Form;

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

/**
 * Class for company config form.
 */
class CompanyConfigForm extends ConfigFormBase {
  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'company_config_form';
   }

   /**
    * {@inheritdoc}
    */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);
    $config = $this->config('company.settings');
    $form['email'] = [
      '#type' => 'textfield',
      '#title' => $this->t('Email'),
      '#default_value' => $config->get('company.email'),
      '#required' => TRUE,
    ];

    $node_types = \Drupal\node\Entity\NodeType::loadMultiple();
    $node_type_titles = [];
    foreach ($node_types as $machine_name => $val) {
      $node_type_titles[$machine_name] = $val->label();
    }

    $form['node_types'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Node Types'),
      '#options' => $node_type_titles,
      '#default_value' => $config->get('company.node_types'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = $this->config('company.settings');
    $config->set('company.email', $form_state->getValue('email'));
    $config->set('company.node_types', $form_state->getValue('node_types'));
    $config->save();
    return parent::submitForm($form, $form_state);
  }

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

getFormId()-> Here id is returned that can be used in hook_alter method to modify things.
buildForm()-> Creates 2 items.
  1. Simple Text Field that is required which is to accept email address.
  2. A group of checkboxes.
submitForm()-> In this method, load our configuration and save the values that are found in form_state. Then we pass the flow to parent object so that it can flow through the stack.

Now when we access path "/admin/config/companyconfig" then the above created form will be available to display as shown in screenshot.




To display the above created path in the admin section like I want to display the path "/admin/config/companyconfig" as menu item in admin > configuration > Add Company Configuration, See Add custom path in menu item.






No comments:

Post a Comment