Completed
Pull Request — 8.x-2.x (#12)
by Frédéric G.
05:26 queued 02:33
created

ConfigForm::buildForm()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 12
nc 3
nop 2
dl 0
loc 17
rs 9.4285
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\Form\ConfigFormBase;
7
use Drupal\Core\Form\FormStateInterface;
8
9
class ConfigForm extends ConfigFormBase {
0 ignored issues
show
introduced by
Missing class doc comment
Loading history...
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) {
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...
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
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...
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
}
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...
70