====== Formulaire de Configuration du Module ======
===== Formulaire =====
Fichier **web/modules/custom/offer/src/Form/CustomConfigForm.php**
namespace Drupal\offer\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Class CustomConfigForm.
*/
class CustomConfigForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'offer.customconfig',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'custom_config_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('offer.customconfig');
$form['analytics'] = [
'#type' => 'details',
'#title' => $this->t('Marketing & analytics'),
'#open' => TRUE,
];
$form['analytics']['tagmanager'] = [
'#type' => 'textarea',
'#title' => $this->t('Tagmanager code'),
'#default_value' => $config->get('tagmanager'),
'#maxlength' => NULL,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('offer.customconfig')
->set('tagmanager', $form_state->getValue('tagmanager'))
->save();
}
}
===== Point de menu =====
Fichier **web/modules/custom/offer/offer.links.menu.yml**
offer.config:
title: 'Offer global settings'
route_name: offer.config
description: 'Global settings for the offer platform'
parent: system.admin_config_system
weight: 99
===== Route =====
Fichier **web/modules/custom/offer/offer.routing.yml**
offer.config:
path: '/admin/config/offer/adminsettings'
defaults:
_form: '\Drupal\offer\Form\CustomConfigForm'
_title: 'Offer platform global settings'
requirements:
_permission: 'administer site configuration'
===== Lecture de la configuration =====
Lire une valeur de la configuration et la rendre disponible dans le template twig :
/**
* Implements hook_preprocess_html().
*/
function MONMODULE_preprocess_html(&$variables) {
$variables['tagmanager'] = \Drupal::config('offer.customconfig')->get('tagmanager');
}
La variable ''tagmanager'' est intégrée au template.
{{ tagmanager }}