ConfigForm.php in MongoDB 8.2
File
modules/mongodb_watchdog/src/Form/ConfigForm.php
View source
<?php
declare (strict_types=1);
namespace Drupal\mongodb_watchdog\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\mongodb_watchdog\Logger;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfigForm extends ConfigFormBase {
protected $typed;
public function __construct(ConfigFactoryInterface $configFactory, array $typed) {
parent::__construct($configFactory);
$this->typed = $typed;
}
public static function create(ContainerInterface $container) : self {
return new static($container
->get('config.factory'), $container
->get('config.typed')
->getDefinition('mongodb_watchdog.settings'));
}
public function buildForm(array $form, FormStateInterface $formState) : array {
$config = $this
->config(Logger::CONFIG_NAME);
foreach ($config
->getRawData() as $key => $default) {
if (mb_substr($key, 0, 1) === '_') {
continue;
}
$schema = $this->typed['mapping'][$key];
list($title, $description) = explode(': ', $schema['label']);
$form[$key] = [
'#default_value' => $default,
'#description' => $description,
'#title' => $title,
];
switch ($schema['type']) {
case 'integer':
$form[$key] += [
'#max' => $schema['max'] ?? PHP_INT_MAX,
'#min' => $schema['min'] ?? 0,
'#type' => 'number',
];
break;
case 'boolean':
$form[$key] += [
'#type' => 'checkbox',
];
break;
default:
break;
}
}
$parentedForm = parent::buildForm($form, $formState);
return $parentedForm;
}
public function submitForm(array &$form, FormStateInterface $formState) : void {
$config = $this
->config(Logger::CONFIG_NAME);
foreach (array_keys($config
->getRawData()) as $key) {
$config
->set($key, intval($formState
->getValue($key)));
}
$config
->save();
$this
->messenger()
->addMessage($this
->t('The configuration options have been saved.'));
}
protected function getEditableConfigNames() : array {
return [
'mongodb_watchdog.settings',
];
}
public function getFormId() : string {
return 'mongodb_watchdog_config';
}
}
Classes
Name |
Description |
ConfigForm |
Class ConfigForm provides configuration for the MongoDB watchdog module. |