in this article, I'll show you how to create a simple configuration form in Drupal 8.
This type of form is used to create forms for configuration pages on your Drupal website. You can set up configuration forms that allow you to make changes to features or views or other configuration entities.
codimth_form_api.info.yml
add configure option to your .info.yml file
configure: codimth.admin_settings_form
codimth_form_api.routing.yml
codimth.admin_settings_form:
path: '/admin/config/codimth/adminsettings'
defaults:
_form: '\Drupal\codimth_form_api\Form\CodimthConfigForm'
_title: 'CodimthConfigForm'
requirements:
_permission: 'access administration pages'
options:
_admin_route: TRUE
src/Form/CodimthConfigForm.php
this class contains :
getFormId(): This needs to return a string that is the unique ID of your form. Namespace the form ID based on your module's name.
buildForm(): This returns a Form API array that defines each of the elements of your form.
submitForm(): collect data and do things, like save the data to the database, send an email, or any number of other operations.
getEditableConfigNames(): Gets the configuration names that will be editable.
<?php
namespace Drupal\codimth_form_api\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements a codimth Config Form API.
*/
class CodimthConfigForm extends ConfigFormBase
{
/**
* @return string
*/
public function getFormId()
{
return 'codimth_form_api_admin_settings';
}
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames()
{
return [
'codimth_form_api.settings',
];
}
/**
* @param array $form
* @param FormStateInterface $form_state
* @return array
*/
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = $this->config('codimth_form_api.settings');
$form['message'] = [
'#type' => 'textarea',
'#title' => $this->t('Welcome message'),
'#description' => $this->t('Welcome message display to users when they login'),
'#default_value' => $config->get('message'),
];
return parent::buildForm($form, $form_state);
}
/**
* @param array $form
* @param FormStateInterface $form_state
*/
public function submitForm(array &$form, FormStateInterface $form_state)
{
$config = $this->config('codimth_form_api.settings');
$config->set('message', $form_state->getValue('message'))->save();
}
}
codimth_form_api.module
<?php
/**
* @param $account
*/
function codimth_form_api_user_login($account) {
$config = \Drupal::config('codimth_form_api.settings');
\Drupal::messenger()->addStatus($config->get('message'));
}
Setting and Getting Configuration Objects
In the previous section, you have set the value for a configuration object in the submit function. And then in the method which defines the form, you have retrieved the configuration and configuration setting.
$config = $this->config('codimth_form_api.settings'): retrive the configuration.
$config->get('message') : get the configuration setting.
$config->set('message', $form_state->getValue('message')) : set the configuration setting.