EmbedSettingsForm::getFormId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\embed\Form;
4
5
use Drupal\Core\Config\ConfigFactoryInterface;
6
use Symfony\Component\DependencyInjection\ContainerInterface;
7
use Drupal\Core\Form\ConfigFormBase;
8
use Drupal\Core\Form\FormStateInterface;
9
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
10
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
11
12
/**
13
 * Configure embed settings for this site.
14
 */
15
class EmbedSettingsForm extends ConfigFormBase {
16
17
  /**
18
   * The stream wrapper manager.
19
   *
20
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
21
   */
22
  protected $streamWrapperManager;
23
24
  /**
25
   * Constructs a EmbedSettingsForm object.
26
   *
27
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
28
   *   The factory for configuration objects.
29
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
30
   *   The stream wrapper manager.
31
   */
32
  public function __construct(ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
33
    parent::__construct($config_factory);
34
    $this->streamWrapperManager = $stream_wrapper_manager;
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('stream_wrapper_manager')
44
    );
45
  }
46
47
  /**
48
   * {@inheritdoc}
49
   */
50
  public function getFormId() {
51
    return 'embed_settings';
52
  }
53
54
  /**
55
   * {@inheritdoc}
56
   */
57
  protected function getEditableConfigNames() {
58
    return ['embed.settings'];
59
  }
60
61
  /**
62
   * {@inheritdoc}
63
   */
64
  public function buildForm(array $form, FormStateInterface $form_state) {
65
    $config = $this->config('embed.settings');
66
67
    $scheme_options = $this->streamWrapperManager->getNames(StreamWrapperInterface::WRITE_VISIBLE);
68
    $form['file_scheme'] = [
69
      '#type' => 'radios',
70
      '#title' => $this->t('Upload destination'),
71
      '#options' => $scheme_options,
72
      '#default_value' => $config->get('file_scheme'),
73
      '#description' => $this->t('Select where the uploaded button icon files should be stored.'),
74
    ];
75
76
    $form['upload_directory'] = [
77
      '#type' => 'textfield',
78
      '#title' => $this->t('File directory'),
79
      '#default_value' => $config->get('upload_directory'),
80
      '#description' => $this->t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
81
      '#element_validate' => [[get_class($this), 'validateDirectory']],
82
    ];
83
84
    return parent::buildForm($form, $form_state);
85
  }
86
87
  /**
88
   * Form API callback.
89
   *
90
   * Removes slashes from the beginning and end of the destination value and
91
   * ensures that the file directory path is not included at the beginning of the
92
   * value.
93
   *
94
   * This function is assigned as an #element_validate callback in
95
   * fieldSettingsForm().
96
   */
97
  public static function validateDirectory($element, FormStateInterface $form_state) {
98
    // Strip slashes from the beginning and end of $element['file_directory'].
99
    $value = trim($element['#value'], '\\/');
100
    $form_state->setValueForElement($element, $value);
101
  }
102
103
  /**
104
   * {@inheritdoc}
105
   */
106
  public function submitForm(array &$form, FormStateInterface $form_state) {
107
    $config = $this->config('embed.settings');
108
    $config->set('file_scheme', $form_state->getValue('file_scheme'));
109
    $config->set('upload_directory', $form_state->getValue('upload_directory'));
110
    $config->save();
111
112
    parent::submitForm($form, $form_state);
113
  }
114
115
}
116