In this blog we will create a new menu group on the admin/config page and create a link within the group to our site's configuration form page. This can be extended easily to contain multiple links should your own website need further configuration links leading to configuration forms.
To achieve our goal we will need to create a new custom module. I will be using a Drupal 8 clean build which has been built with the hussainweb/drupal-composer-init composer template. This template has the Drupal Console and Drush built in for additional convenience.
$ drupal generate:module \ > --module="myconfig" \ > --machine-name="myconfig" \ > --module-path="modules/custom" \ > --description="Sample Configuration Administration Code" \ > --core="8.x" \ > --package="Custom" \ > --learning \ > --uri="http://default" \ > --no-interaction
$ cd /var/www/html/clean/docroot/modules/custom/myconfig $ tree . └── myconfig.info.yml $ cat myconfig.info.yml name: 'myconfig' type: module description: 'Sample Configuration Administration Code' core: 8.x package: 'Custom'
$ drupal generate:form:config \ > --module="myconfig" \ > --class="ConfigurationForm" \ > --form-id="myconfig_admin_settings_form" \ > --inputs='"name":"config_input_1", "type":"text_format", "label":"Config Input 1", "options":"", "description":"Sample Text Input 1", "maxlength":"", "size":"", "default_value":"", "weight":"0", "fieldset":""' \ > --inputs='"name":"config_input_2", "type":"text_format", "label":"Config Input 2", "options":"", "description":"Sample Text Input 2", "maxlength":"", "size":"", "default_value":"", "weight":"0", "fieldset":""' \ > --path="/admin/config/myconfig/configuration" \ > --menu-link-gen \ > --menu-link-title="Sample Configuration Form" \ > --menu-parent="myconfig.group.admin" \ > --menu-link-desc="Configure MySite" \ > --learning --uri="http://default" \ > --no-interaction
$ tree
.
├── myconfig.info.yml
├── myconfig.links.menu.yml
├── myconfig.routing.yml
└── src
└── Form
└── ConfigurationForm.phpmyconfig.links.menu.yml
myconfig.myconfig_admin_settings_form: title: 'Sample Configuration Form' route_name: myconfig.myconfig_admin_settings_form description: 'Configure MySite' parent: system.admin_config weight: 99
myconfig.routing.yml
myconfig.myconfig_admin_settings_form: path: '/admin/config/myconfig/configuration' defaults: _form: '\Drupal\myconfig\Form\ConfigurationForm' _title: 'ConfigurationForm' requirements: _permission: 'access administration pages' options: _admin_route: TRUE
ConfigurationForm.php
<?php
namespace Drupal\myconfig\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class ConfigurationForm.
*/
class ConfigurationForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'myconfig.configuration',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'myconfig_admin_settings_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('myconfig.configuration');
$form['config_input_1'] = [
'#type' => 'text_format',
'#title' => $this->t('Config Input 1'),
'#description' => $this->t('Sample Text Input 1'),
'#default_value' => $config->get('config_input_1'),
];
$form['config_input_2'] = [
'#type' => 'text_format',
'#title' => $this->t('Config Input 2'),
'#description' => $this->t('Sample Text Input 2'),
'#default_value' => $config->get('config_input_2'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('myconfig.configuration')
->set('config_input_1', $form_state->getValue('config_input_1'))
->set('config_input_2', $form_state->getValue('config_input_2'))
->save();
}
}
?>Ok - time to enable our module to see if it actually works!
$ drush en myconfig -y [success] Successfully enabled: myconfig
By navigating to admin/config/myconfig/configuration the config form can be seen. As part of the Drupal Console command to generate the form I specified two text_format fields which can be clearly seen. Furthermore, any values added to the fields will be saved as the module's configuration once the form is submitted, and re-presented to the user for editing on subsequent loads of the page.
However, we aren't done yet. We want our own group of menu items on the admin/config page and we haven't achieved that so far...
myconfig.links.menu.yml - add to the bottom
myconfig.group.admin: title: 'Mysite' route_name: system.admin_config_myconfig parent: system.admin_config description: 'Myconfig Configuration' weight: -999
myconfig.routing.yml - add to the bottom
system.admin_config_myconfig: path: '/admin/config/mysite' defaults: _controller: '\Drupal\system\Controller\SystemController::systemAdminMenuBlockPage' _title: 'Mysite' requirements: _permission: 'access administration pages'
$ drush cr [success] Cache rebuild complete.
If we now navigate to admin/config/mysite (above screenshot) and admin/config/myconfig/configuration (screenshot at the page top) we can see that we have succeeded! Success without writing a single line of PHP code - all our PHP was generated by Drupal Console!