Completed
Pull Request — 8.x-2.x (#16)
by Frédéric G.
05:47 queued 02:42
created

ConfigForm::buildForm()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 5
eloc 26
c 1
b 0
f 1
nc 5
nop 2
dl 0
loc 37
rs 8.439
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
        '#title' => $title,
62
      ];
63
64
      switch ($schema['type']) {
65
        case 'integer':
66
          $form[$key] += [
67
            '#max' => $schema['max'] ?? PHP_INT_MAX,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
68
            '#min' => $schema['min'] ?? 0,
1 ignored issue
show
introduced by
Expected 1 space after "?"; 0 found
Loading history...
69
            '#type' => 'number',
70
          ];
71
          break;
72
73
        case 'boolean':
1 ignored issue
show
introduced by
Expected 1 space before ":"; 0 found
Loading history...
74
          $form[$key] += [
75
            '#type' => 'checkbox',
76
          ];
77
          break;
78
79
        default:
1 ignored issue
show
introduced by
Expected 1 space before ":"; 0 found
Loading history...
80
          break;
81
      }
82
    }
83
84
    $form = parent::buildForm($form, $form_state);
85
    return $form;
86
  }
87
88
  /**
89
   * {@inheritdoc}
90
   */
91
  public function submitForm(array &$form, FormStateInterface $form_state) {
92
    $config = $this->config(Logger::CONFIG_NAME);
93
    foreach (array_keys($config->getRawData()) as $key) {
94
      $config->set($key, intval($form_state->getValue($key)));
95
    }
96
    $config->save();
97
    drupal_set_message($this->t('The configuration options have been saved.'));
98
  }
99
100
  /**
101
   * Gets the configuration names that will be editable.
102
   *
103
   * @return string[]
104
   *   An array of configuration object names that are editable if called in
105
   *   conjunction with the trait's config() method.
106
   */
107
  protected function getEditableConfigNames() {
108
    return ['mongodb_watchdog.settings'];
109
  }
110
111
  /**
112
   * Returns a unique string identifying the form.
113
   *
114
   * @return string
115
   *   The unique string identifying the form.
116
   */
117
  public function getFormId() {
118
    return 'mongodb_watchdog_config';
119
  }
120
121
}
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...
122