Completed
Push — 8.x-2.x ( 2edb6e...14a51c )
by Frédéric G.
14:20 queued 11:07
created

ConfigForm::getFormId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 1
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 Drupal\mongodb_watchdog\Logger;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Class ConfigForm provides configuration for the MongoDB watchdog module.
14
 */
15
class ConfigForm extends ConfigFormBase {
16
17
  /**
18
   * Typed schema for the configuration.
19
   *
20
   * @var array
21
   */
22
  protected $typed;
23
24
  /**
25
   * ConfigForm constructor.
26
   *
27
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
28
   *   The core config.factory service.
29
   * @param array $typed
30
   *   The type config for the module.
31
   */
32
  public function __construct(ConfigFactoryInterface $config_factory, array $typed) {
33
    parent::__construct($config_factory);
34
    $this->typed = $typed;
35
  }
36
37
  /**
38
   * {@inheritdoc}
39
   */
40
  public static function create(ContainerInterface $container) {
41
    return new static(
42
      $container->get('config.factory'),
43
      $container->get('config.typed')->getDefinition('mongodb_watchdog.settings')
44
    );
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  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...
51
    $config = $this->config(Logger::CONFIG_NAME);
52
    foreach ($config->getRawData() as $key => $default_value) {
53
      if (Unicode::substr($key, 0, 1) === '_') {
54
        continue;
55
      }
56
      $schema = $this->typed['mapping'][$key];
57
      list($title, $description) = explode(': ', $schema['label']);
58
      $form[$key] = [
59
        '#default_value' => $default_value,
60
        '#description' => $description,
61
        '#max' => $schema['max'] ?? PHP_INT_MAX,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
62
        '#min' => $schema['min'] ?? 0,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
63
        '#title' => $title,
64
        '#type' => 'number',
65
      ];
66
    }
67
68
    $form = parent::buildForm($form, $form_state);
69
    return $form;
70
  }
71
72
  /**
73
   * {@inheritdoc}
74
   */
75
  public function submitForm(array &$form, FormStateInterface $form_state) {
76
    $config = $this->config(Logger::CONFIG_NAME);
77
    foreach (array_keys($config->getRawData()) as $key) {
78
      $config->set($key, intval($form_state->getValue($key)));
79
    }
80
    $config->save();
81
    drupal_set_message($this->t('The configuration options have been saved.'));
82
  }
83
84
  /**
85
   * Gets the configuration names that will be editable.
86
   *
87
   * @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...
88
   *   An array of configuration object names that are editable if called in
89
   *   conjunction with the trait's config() method.
90
   */
91
  protected function getEditableConfigNames() {
92
    return ['mongodb_watchdog.settings'];
93
  }
94
95
  /**
96
   * Returns a unique string identifying the form.
97
   *
98
   * @return string
99
   *   The unique string identifying the form.
100
   */
101
  public function getFormId() {
102
    return 'mongodb_watchdog_config';
103
  }
104
105
}
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...
106