class BootstrapLayoutsBase in Bootstrap Layouts 8.4
Same name and namespace in other branches
- 8.5 src/Plugin/Layout/BootstrapLayoutsBase.php \Drupal\bootstrap_layouts\Plugin\Layout\BootstrapLayoutsBase
Layout class for all bootstrap layouts.
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\layout_plugin\Plugin\Layout\LayoutBase implements ConfigurablePluginInterface, PluginFormInterface, LayoutInterface
- class \Drupal\bootstrap_layouts\Plugin\Layout\BootstrapLayoutsBase
- class \Drupal\layout_plugin\Plugin\Layout\LayoutBase implements ConfigurablePluginInterface, PluginFormInterface, LayoutInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of BootstrapLayoutsBase
1 file declares its use of BootstrapLayoutsBase
1 string reference to 'BootstrapLayoutsBase'
File
- src/
Plugin/ Layout/ BootstrapLayoutsBase.php, line 14
Namespace
Drupal\bootstrap_layouts\Plugin\LayoutView source
class BootstrapLayoutsBase extends LayoutBase {
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* Provides a default region definition.
*
* @return array
* Default region array.
*/
protected function getRegionDefaults() {
return [
'wrapper' => 'div',
'classes' => [],
'attributes' => '',
'add_region_classes' => TRUE,
];
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
$configuration += [
'layout' => [
'wrapper' => 'div',
'classes' => [
'row',
],
'attributes' => '',
'add_layout_class' => TRUE,
],
'regions' => [],
];
foreach ($this
->getRegionDefinitions() as $region => $info) {
$region_configuration = [];
foreach ([
'wrapper',
'classes',
'attributes',
] as $key) {
if (isset($info[$key])) {
$region_configuration[$key] = $info[$key];
}
}
$configuration['regions'][$region] = $region_configuration + $this
->getRegionDefaults();
}
return $configuration;
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
$configuration = $this
->defaultConfiguration();
// Can't use parent::getConfiguration() here because array_merge() only
// merges the top levels. Nor can NestedArray::mergeDeep be used since it
// will add multiple classes (from default + config). Instead, the two
// top levels "layout" and "regions" must be merged using array_merge().
if (isset($this->configuration['layout'])) {
$configuration['layout'] = array_merge($configuration['layout'], $this->configuration['layout']);
}
if (isset($this->configuration['regions'])) {
$configuration['regions'] = array_merge($configuration['regions'], $this->configuration['regions']);
}
// Remove any region configuration that doesn't apply to current layout.
$regions = $this
->getRegionNames();
foreach (array_keys($configuration['regions']) as $region) {
if (!isset($regions[$region])) {
unset($configuration['regions'][$region]);
}
}
return $configuration;
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$configuration = $this
->getConfiguration();
/** @var \Drupal\bootstrap_layouts\BootstrapLayoutsManager $bootstrap_layouts_manager */
$manager = \Drupal::getContainer()
->get('plugin.manager.bootstrap_layouts');
$classes = $manager
->getClassOptions();
$tokens = FALSE;
if (\Drupal::moduleHandler()
->moduleExists('token')) {
$tokens = [
'#title' => $this
->t('Tokens'),
'#type' => 'container',
];
$tokens['help'] = [
'#theme' => 'token_tree_link',
'#token_types' => 'all',
'#global_types' => FALSE,
'#dialog' => TRUE,
];
}
// Add wrappers.
$wrapper_options = [
'div' => 'Div',
'span' => 'Span',
'section' => 'Section',
'article' => 'Article',
'header' => 'Header',
'footer' => 'Footer',
'aside' => 'Aside',
'figure' => 'Figure',
];
$form['layout'] = [
'#type' => 'container',
'#tree' => TRUE,
];
$form['layout']['wrapper'] = [
'#type' => 'select',
'#title' => $this
->t('Wrapper'),
'#options' => $wrapper_options,
'#default_value' => $form_state
->getValue([
'layout',
'wrapper',
], $configuration['layout']['wrapper']),
];
$form['layout']['classes'] = [
'#type' => 'select',
'#title' => $this
->t('Classes'),
'#options' => $classes,
'#default_value' => $form_state
->getValue([
'layout',
'classes',
], $configuration['layout']['classes']) ?: [],
'#multiple' => TRUE,
];
$form['layout']['add_layout_class'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add layout specific class: <code>@class</code>', [
'@class' => Html::cleanCssIdentifier($this
->getPluginId()),
]),
'#default_value' => (int) $form_state
->getValue([
'layout',
'add_layout_class',
], $configuration['layout']['add_layout_class']),
];
$form['layout']['attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Additional attributes'),
'#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
'#default_value' => $form_state
->getValue([
'layout',
'attributes',
], $configuration['layout']['attributes']),
];
if ($tokens) {
$form['layout']['tokens'] = $tokens;
}
// Add each region's settings.
foreach ($this
->getRegionNames() as $region => $region_label) {
$default_values = NestedArray::mergeDeep($this
->getRegionDefaults(), isset($configuration['regions'][$region]) ? $configuration['regions'][$region] : [], $form_state
->getValue([
'regions',
$region,
], []));
$form[$region] = [
'#group' => 'additional_settings',
'#type' => 'details',
'#open' => TRUE,
'#title' => $this
->t('Region: @region', [
'@region' => $region_label,
]),
'#weight' => 20,
];
$form[$region]['wrapper'] = [
'#type' => 'select',
'#title' => $this
->t('Wrapper'),
'#options' => $wrapper_options,
'#default_value' => $default_values['wrapper'],
];
$form[$region]['classes'] = [
'#type' => 'select',
'#title' => $this
->t('Classes'),
'#options' => $classes,
'#default_value' => $default_values['classes'],
'#multiple' => TRUE,
];
$form[$region]['add_region_classes'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Add region specific classes: <code>bs-region</code> and <code>bs-region--@region</code>', [
'@region' => $region,
]),
'#default_value' => (int) $default_values['add_region_classes'],
];
$form[$region]['attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Additional attributes'),
'#description' => 'E.g. id|custom-id,role|navigation,data-something|some value',
'#default_value' => $default_values['attributes'],
];
if ($tokens) {
$form[$region]['tokens'] = $tokens;
}
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$defaults = $this
->getRegionDefaults();
if ($layout = $form_state
->getValue('layout', $defaults)) {
// Apply Xss::filter to attributes.
$layout['attributes'] = Xss::filter($layout['attributes']);
$this->configuration['layout'] = $layout;
}
$regions = [];
foreach (array_keys($this
->getRegionNames()) as $name) {
if ($region = $form_state
->getValue($name, $defaults)) {
// Apply Xss::filter to attributes.
$region['attributes'] = Xss::filter($region['attributes']);
$regions[$name] = $region;
}
}
$this->configuration['regions'] = $regions;
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BootstrapLayoutsBase:: |
public | function |
Form constructor. Overrides LayoutBase:: |
|
BootstrapLayoutsBase:: |
public | function |
Gets default configuration for this plugin. Overrides LayoutBase:: |
|
BootstrapLayoutsBase:: |
public | function |
Gets this plugin's configuration. Overrides LayoutBase:: |
|
BootstrapLayoutsBase:: |
protected | function | Provides a default region definition. | |
BootstrapLayoutsBase:: |
public | function |
Form submission handler. Overrides LayoutBase:: |
|
BootstrapLayoutsBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
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 | |
LayoutBase:: |
protected | property |
The layout configuration. Overrides PluginBase:: |
|
LayoutBase:: |
public | function |
Build a render array for layout with regions. Overrides LayoutInterface:: |
|
LayoutBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
LayoutBase:: |
public | function | Gets the path to resources like icon or template. | |
LayoutBase:: |
public | function | Gets the human-readable category. | |
LayoutBase:: |
public | function | Gets the optional description for advanced layouts. | |
LayoutBase:: |
public | function | Gets the path to the preview image. | |
LayoutBase:: |
public | function | Gets the human-readable name. | |
LayoutBase:: |
public | function | Get the asset library. | |
LayoutBase:: |
public | function | Gets information on regions keyed by machine name. | |
LayoutBase:: |
public | function | Gets human-readable list of regions keyed by machine name. | |
LayoutBase:: |
public | function | Gets the theme hook used to render this layout. | |
LayoutBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface:: |
|
LayoutBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
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. | |
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. |