Completed
Push — watchdog-config_level ( 2edb6e )
by Frédéric G.
14:05 queued 11:29
created

ConfigForm::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
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,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
62
        '#max' => $schema['max'] ?? PHP_INT_MAX,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
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
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
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
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
105