class SystemThemeSettings in Express 8
Implements hook_form_system_theme_settings_alter().
Plugin annotation
@BootstrapForm("system_theme_settings");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\bootstrap\Plugin\Form\FormBase implements FormInterface
- class \Drupal\bootstrap\Plugin\Form\SystemThemeSettings implements FormInterface
- class \Drupal\bootstrap\Plugin\Form\FormBase implements FormInterface
- class \Drupal\bootstrap\Plugin\PluginBase
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of SystemThemeSettings
1 file declares its use of SystemThemeSettings
- Schemas.php in themes/
contrib/ bootstrap/ src/ Plugin/ Setting/ Schemas.php - Contains \Drupal\bootstrap\Plugin\Setting\Schemas.
File
- themes/
contrib/ bootstrap/ src/ Plugin/ Form/ SystemThemeSettings.php, line 22 - Contains \Drupal\bootstrap\Plugin\Form\SystemThemeSettings.
Namespace
Drupal\bootstrap\Plugin\FormView source
class SystemThemeSettings extends FormBase implements FormInterface {
/**
* {@inheritdoc}
*/
public function alterFormElement(Element $form, FormStateInterface $form_state, $form_id = NULL) {
$theme = $this
->getTheme($form, $form_state);
if (!$theme) {
return;
}
// Creates the necessary groups (vertical tabs) for a Bootstrap based theme.
$this
->createGroups($form, $form_state);
// Iterate over all setting plugins and add them to the form.
foreach ($theme
->getSettingPlugin() as $setting) {
$setting
->alterForm($form
->getArray(), $form_state);
}
}
/**
* Sets up the vertical tab groupings.
*
* @param \Drupal\bootstrap\Utility\Element $form
* The Element object that comprises the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
protected function createGroups(Element $form, FormStateInterface $form_state) {
// Vertical tabs for global settings provided by core or contrib modules.
if (!isset($form['global'])) {
$form['global'] = [
'#type' => 'vertical_tabs',
'#weight' => -9,
'#prefix' => '<h2><small>' . t('Override Global Settings') . '</small></h2>',
];
}
// Iterate over existing children and move appropriate ones to global group.
foreach ($form
->children() as $child) {
if ($child
->isType([
'details',
'fieldset',
]) && !$child
->hasProperty('group')) {
$child
->setProperty('type', 'details');
$child
->setProperty('group', 'global');
}
}
// Provide the necessary default groups.
$form['bootstrap'] = [
'#type' => 'vertical_tabs',
'#attached' => [
'library' => [
'bootstrap/theme-settings',
],
],
'#prefix' => '<h2><small>' . t('Bootstrap Settings') . '</small></h2>',
'#weight' => -10,
];
$groups = [
'general' => t('General'),
'components' => t('Components'),
'javascript' => t('JavaScript'),
'advanced' => t('Advanced'),
];
foreach ($groups as $group => $title) {
$form[$group] = [
'#type' => 'details',
'#title' => $title,
'#group' => 'bootstrap',
];
}
}
/**
* Retrieves the currently selected theme on the settings form.
*
* @param \Drupal\bootstrap\Utility\Element $form
* The Element object that comprises the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*
* @return \Drupal\bootstrap\Theme|FALSE
* The currently selected theme object or FALSE if not a Bootstrap theme.
*/
public static function getTheme(Element $form, FormStateInterface $form_state) {
$build_info = $form_state
->getBuildInfo();
$theme = isset($build_info['args'][0]) ? Bootstrap::getTheme($build_info['args'][0]) : FALSE;
// Do not continue if the theme is not Bootstrap specific.
if (!$theme || !$theme
->isBootstrap()) {
unset($form['#submit'][0]);
unset($form['#validate'][0]);
}
return $theme;
}
/**
* {@inheritdoc}
*/
public static function submitFormElement(Element $form, FormStateInterface $form_state) {
$theme = self::getTheme($form, $form_state);
if (!$theme) {
return;
}
$cache_tags = [];
$save = FALSE;
$settings = $theme
->settings();
// Iterate over all setting plugins and manually save them since core's
// process is severely limiting and somewhat broken.
foreach ($theme
->getSettingPlugin() as $name => $setting) {
// Allow the setting to participate in the form submission process.
// Must call the "submitForm" method in case any setting actually uses it.
// It should, in turn, invoke "submitFormElement", if the setting that
// overrides it is implemented properly.
$setting
->submitForm($form
->getArray(), $form_state);
// Retrieve the submitted value.
$value = $form_state
->getValue($name);
// Determine if the setting has a new value that overrides the original.
// Ignore the schemas "setting" because it's handled by UpdateManager.
if ($name !== 'schemas' && $settings
->overridesValue($name, $value)) {
// Set the new value.
$settings
->set($name, $value);
// Retrieve the cache tags for the setting.
$cache_tags = array_unique(array_merge($setting
->getCacheTags()));
// Flag the save.
$save = TRUE;
}
// Remove value from the form state object so core doesn't re-save it.
$form_state
->unsetValue($name);
}
// Save the settings, if needed.
if ($save) {
$settings
->save();
// Invalidate necessary cache tags.
if ($cache_tags) {
\Drupal::service('cache_tags.invalidator')
->invalidateTags($cache_tags);
}
// Clear our internal theme cache so it can be rebuilt properly.
$theme
->getCache('settings')
->deleteAll();
}
}
/**
* {@inheritdoc}
*/
public static function validateFormElement(Element $form, FormStateInterface $form_state) {
$theme = self::getTheme($form, $form_state);
if (!$theme) {
return;
}
// Iterate over all setting plugins and allow them to participate.
foreach ($theme
->getSettingPlugin() as $setting) {
// Allow the setting to participate in the form validation process.
// Must call the "validateForm" method in case any setting actually uses it.
// It should, in turn, invoke "validateFormElement", if the setting that
// overrides it is implemented properly.
$setting
->validateForm($form
->getArray(), $form_state);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
public | function |
The alter method to store the code. Overrides FormInterface:: |
|
FormBase:: |
public static | function |
Form submission handler. Overrides FormInterface:: |
|
FormBase:: |
public static | function |
Form validation handler. Overrides FormInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
protected | property | The currently set theme object. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
1 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
SystemThemeSettings:: |
public | function |
The alter method to store the code. Overrides FormBase:: |
|
SystemThemeSettings:: |
protected | function | Sets up the vertical tab groupings. | |
SystemThemeSettings:: |
public static | function | Retrieves the currently selected theme on the settings form. | |
SystemThemeSettings:: |
public static | function |
Form submission handler. Overrides FormBase:: |
|
SystemThemeSettings:: |
public static | function |
Form validation handler. Overrides FormBase:: |