SettingsForm.php in Bootstrap Styles 1.0.x
File
src/Form/SettingsForm.php
View source
<?php
namespace Drupal\bootstrap_styles\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\bootstrap_styles\StylesGroup\StylesGroupManager;
use Drupal\bootstrap_styles\Style\StyleManager;
class SettingsForm extends ConfigFormBase {
protected $stylesGroupManager;
protected $styleManager;
public function __construct(StylesGroupManager $styles_group_manager, StyleManager $style_manager) {
$this->stylesGroupManager = $styles_group_manager;
$this->styleManager = $style_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.manager.bootstrap_styles_group'), $container
->get('plugin.manager.bootstrap_styles'));
}
const SETTINGS = 'bootstrap_styles.settings';
public function getFormId() {
return 'bootstrap_styles_admin_settings';
}
protected function getEditableConfigNames() {
return [
static::SETTINGS,
];
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$this
->messenger()
->addWarning('Important: the following classes are only used as an example to explain how to use this module. You should use your own theme classes.');
$options = [
'dark' => $this
->t('Dark'),
'light' => $this
->t('Light'),
];
$form['layout_builder_theme'] = [
'#type' => 'select',
'#title' => $this
->t('Layout Builder Theme'),
'#options' => $options,
'#default_value' => $this
->config(static::SETTINGS)
->get('layout_builder_theme') ?? 'dark',
];
foreach ($this->stylesGroupManager
->getStylesGroups() as $group_plugin_id => $styles_group) {
$group_instance = $this->stylesGroupManager
->createInstance($group_plugin_id);
$form = $group_instance
->buildConfigurationForm($form, $form_state);
if (isset($styles_group['styles'])) {
foreach (array_keys($styles_group['styles']) as $style_plugin_id) {
$style_instance = $this->styleManager
->createInstance($style_plugin_id);
$form = $style_instance
->buildConfigurationForm($form, $form_state);
}
}
}
$form['#attached']['library'][] = 'bootstrap_styles/layout_builder_admin_form_style';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this
->config(static::SETTINGS)
->set('layout_builder_theme', $form_state
->getValue('layout_builder_theme'))
->save();
foreach ($this->stylesGroupManager
->getStylesGroups() as $group_plugin_id => $style_group) {
$group_instance = $this->stylesGroupManager
->createInstance($group_plugin_id);
$group_instance
->submitConfigurationForm($form, $form_state);
if (isset($style_group['styles'])) {
foreach ($style_group['styles'] as $style_plugin_id => $style) {
$style_instance = $this->styleManager
->createInstance($style_plugin_id);
$style_instance
->submitConfigurationForm($form, $form_state);
}
}
}
}
}