|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drupal\mongodb_watchdog\Form; |
|
4
|
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Unicode; |
|
6
|
|
|
use Drupal\Core\Form\ConfigFormBase; |
|
7
|
|
|
use Drupal\Core\Form\FormStateInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ConfigForm extends ConfigFormBase { |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Name of the config being edited. |
|
13
|
|
|
*/ |
|
14
|
|
|
const CONFIG_NAME = 'mongodb_watchdog.settings'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
public function buildForm(array $form, FormStateInterface $form_state) { |
|
|
|
|
|
|
20
|
|
|
$config = $this->config(static::CONFIG_NAME); |
|
21
|
|
|
foreach ($config->getRawData() as $key => $default_value) { |
|
22
|
|
|
if (Unicode::substr($key, 0, 1) === '_') { |
|
23
|
|
|
continue; |
|
24
|
|
|
} |
|
25
|
|
|
$form[$key] = [ |
|
26
|
|
|
'#title' => Unicode::ucfirst(str_replace('_', ' ', $key)), |
|
27
|
|
|
'#type' => 'number', |
|
28
|
|
|
'#min' => 1, |
|
29
|
|
|
'#default_value' => $default_value, |
|
30
|
|
|
]; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$form = parent::buildForm($form, $form_state); |
|
34
|
|
|
return $form; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function submitForm(array &$form, FormStateInterface $form_state) { |
|
41
|
|
|
$config = $this->config(static::CONFIG_NAME); |
|
42
|
|
|
foreach (array_keys($config->getRawData()) as $key) { |
|
43
|
|
|
$config->set($key, intval($form_state->getValue($key))); |
|
44
|
|
|
} |
|
45
|
|
|
$config->save(); |
|
46
|
|
|
drupal_set_message($this->t('The configuration options have been saved.')); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Gets the configuration names that will be editable. |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
|
|
|
|
|
53
|
|
|
* An array of configuration object names that are editable if called in |
|
54
|
|
|
* conjunction with the trait's config() method. |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getEditableConfigNames() { |
|
57
|
|
|
return ['mongodb_watchdog.settings']; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns a unique string identifying the form. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
* The unique string identifying the form. |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getFormId() { |
|
67
|
|
|
return 'mongodb_watchdog_config'; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
|
|
|
|
|
70
|
|
|
|