Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SettingsForm
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 getFormId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getEditableConfigNames
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 buildForm
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 validateForm
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 submitForm
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors_geoip\Form;
4
5use Drupal\Core\Form\ConfigFormBase;
6use Drupal\Core\Form\FormStateInterface;
7
8/**
9 * Visitors Settings Form.
10 */
11final class SettingsForm extends ConfigFormBase {
12
13  /**
14   * Config settings.
15   *
16   * @var string
17   */
18  const SETTINGS = 'visitors_geoip.settings';
19
20  /**
21   * {@inheritdoc}
22   */
23  public function getFormId() {
24    return 'visitors_geoip_admin_form';
25  }
26
27  /**
28   * {@inheritdoc}
29   */
30  protected function getEditableConfigNames() {
31    return [
32      static::SETTINGS,
33    ];
34  }
35
36  /**
37   * {@inheritdoc}
38   */
39  public function buildForm(array $form, FormStateInterface $form_state) {
40    $settings = $this->configFactory->get('visitors_geoip.settings');
41    $form = parent::buildForm($form, $form_state);
42
43    $form['geoip_path'] = [
44      '#type' => 'textfield',
45      '#title' => $this->t('GeoIP Database path'),
46      '#description' => $this->t('Enter the path to the MindMax database(s). Just the directory, relative to the Drupal root.'),
47      '#default_value' => $settings->get('geoip_path'),
48    ];
49    $form['license'] = [
50      '#type' => 'textfield',
51      '#title' => $this->t('MaxMind License Key'),
52      '#description' => $this->t('Enter your MaxMind license key. If you do not have one, you can get one <a href="https://www.maxmind.com/en/geolite2/signup">here</a>.'),
53      '#default_value' => $settings->get('license'),
54    ];
55
56    return $form;
57  }
58
59  /**
60   * {@inheritdoc}
61   */
62  public function validateForm(array &$form, FormStateInterface $form_state) {
63    $path = $form_state->getValue('geoip_path');
64    if (!is_dir($path)) {
65      $form_state->setErrorByName('geoip_path', $this->t('Directory does not exists, or is not accessible.'));
66    }
67  }
68
69  /**
70   * {@inheritdoc}
71   */
72  public function submitForm(array &$form, FormStateInterface $form_state) {
73    $config = $this->configFactory->getEditable(static::SETTINGS);
74    $values = $form_state->getValues();
75    $config
76      ->set('geoip_path', $values['geoip_path'])
77      ->set('license', $values['license'])
78      ->save();
79
80    parent::submitForm($form, $form_state);
81  }
82
83}