1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Form; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Unicode; |
6
|
|
|
use Drupal\Core\Config\ConfigFactoryInterface; |
7
|
|
|
use Drupal\Core\Form\ConfigFormBase; |
8
|
|
|
use Drupal\Core\Form\FormStateInterface; |
9
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ConfigForm provides configuration for the MongoDB watchdog module. |
13
|
|
|
*/ |
14
|
|
|
class ConfigForm extends ConfigFormBase { |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Name of the config being edited. |
18
|
|
|
*/ |
19
|
|
|
const CONFIG_NAME = 'mongodb_watchdog.settings'; |
20
|
|
|
|
21
|
|
|
protected $typed; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* ConfigForm constructor. |
25
|
|
|
* |
26
|
|
|
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory |
27
|
|
|
* The core config.factory service. |
28
|
|
|
* @param array $typed |
29
|
|
|
* The type config for the module. |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ConfigFactoryInterface $config_factory, array $typed) { |
32
|
|
|
parent::__construct($config_factory); |
33
|
|
|
$this->typed = $typed; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* {@inheritdoc} |
38
|
|
|
*/ |
39
|
|
|
public static function create(ContainerInterface $container) { |
40
|
|
|
return new static( |
41
|
|
|
$container->get('config.factory'), |
42
|
|
|
$container->get('config.typed')->getDefinition('mongodb_watchdog.settings') |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function buildForm(array $form, FormStateInterface $form_state) { |
|
|
|
|
50
|
|
|
$config = $this->config(static::CONFIG_NAME); |
51
|
|
|
foreach ($config->getRawData() as $key => $default_value) { |
52
|
|
|
if (Unicode::substr($key, 0, 1) === '_') { |
53
|
|
|
continue; |
54
|
|
|
} |
55
|
|
|
$schema = $this->typed['mapping'][$key]; |
56
|
|
|
list($title, $description) = explode(': ', $schema['label']); |
57
|
|
|
$form[$key] = [ |
58
|
|
|
'#title' => $title, |
59
|
|
|
'#description' => $description, |
60
|
|
|
'#type' => 'number', |
61
|
|
|
'#min' => $schema['min'] ?? 0, |
|
|
|
|
62
|
|
|
'#max' => $schema['max'] ?? PHP_INT_MAX, |
|
|
|
|
63
|
|
|
'#default_value' => $default_value, |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$form = parent::buildForm($form, $form_state); |
68
|
|
|
return $form; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
*/ |
74
|
|
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
75
|
|
|
$config = $this->config(static::CONFIG_NAME); |
76
|
|
|
foreach (array_keys($config->getRawData()) as $key) { |
77
|
|
|
$config->set($key, intval($form_state->getValue($key))); |
78
|
|
|
} |
79
|
|
|
$config->save(); |
80
|
|
|
drupal_set_message($this->t('The configuration options have been saved.')); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Gets the configuration names that will be editable. |
85
|
|
|
* |
86
|
|
|
* @return array |
|
|
|
|
87
|
|
|
* An array of configuration object names that are editable if called in |
88
|
|
|
* conjunction with the trait's config() method. |
89
|
|
|
*/ |
90
|
|
|
protected function getEditableConfigNames() { |
91
|
|
|
return ['mongodb_watchdog.settings']; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a unique string identifying the form. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
* The unique string identifying the form. |
99
|
|
|
*/ |
100
|
|
|
public function getFormId() { |
101
|
|
|
return 'mongodb_watchdog_config'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
|
|
|
|
105
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.