EmbedSettingsForm.php in Embed 8
File
src/Form/EmbedSettingsForm.php
View source
<?php
namespace Drupal\embed\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
class EmbedSettingsForm extends ConfigFormBase {
protected $streamWrapperManager;
public function __construct(ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
parent::__construct($config_factory);
$this->streamWrapperManager = $stream_wrapper_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('stream_wrapper_manager'));
}
public function getFormId() {
return 'embed_settings';
}
protected function getEditableConfigNames() {
return [
'embed.settings',
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('embed.settings');
$scheme_options = $this->streamWrapperManager
->getNames(StreamWrapperInterface::WRITE_VISIBLE);
$form['file_scheme'] = [
'#type' => 'radios',
'#title' => $this
->t('Upload destination'),
'#options' => $scheme_options,
'#default_value' => $config
->get('file_scheme'),
'#description' => $this
->t('Select where the uploaded button icon files should be stored.'),
];
$form['upload_directory'] = [
'#type' => 'textfield',
'#title' => $this
->t('File directory'),
'#default_value' => $config
->get('upload_directory'),
'#description' => $this
->t('Optional subdirectory within the upload destination where files will be stored. Do not include preceding or trailing slashes.'),
'#element_validate' => [
[
get_class($this),
'validateDirectory',
],
],
];
return parent::buildForm($form, $form_state);
}
public static function validateDirectory($element, FormStateInterface $form_state) {
$value = trim($element['#value'], '\\/');
$form_state
->setValueForElement($element, $value);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$config = $this
->config('embed.settings');
$config
->set('file_scheme', $form_state
->getValue('file_scheme'));
$config
->set('upload_directory', $form_state
->getValue('upload_directory'));
$config
->save();
parent::submitForm($form, $form_state);
}
}