class Margin in Bootstrap Styles 1.0.x
Class Margin.
@package Drupal\bootstrap_styles\Plugin\Style
Plugin annotation
@Style(
id = "margin",
title = @Translation("Margin"),
group_id = "spacing",
weight = 2
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\bootstrap_styles\Style\StylePluginBase implements StylePluginInterface uses StringTranslationTrait
- class \Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style\Margin
- class \Drupal\bootstrap_styles\Style\StylePluginBase implements StylePluginInterface uses StringTranslationTrait
Expanded class hierarchy of Margin
3 string references to 'Margin'
- bootstrap_styles.schema.yml in config/
schema/ bootstrap_styles.schema.yml - config/schema/bootstrap_styles.schema.yml
- Margin::buildConfigurationForm in src/
Plugin/ BootstrapStyles/ Style/ Margin.php - Margin::buildStyleFormElements in src/
Plugin/ BootstrapStyles/ Style/ Margin.php
File
- src/
Plugin/ BootstrapStyles/ Style/ Margin.php, line 20
Namespace
Drupal\bootstrap_styles\Plugin\BootstrapStyles\StyleView source
class Margin extends StylePluginBase {
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$config = $this
->config();
$directions = [
'left',
'top',
'right',
'bottom',
];
$form['spacing']['margin_description'] = [
'#type' => 'item',
'#title' => $this
->t('Margin'),
'#markup' => $this
->t('<p>Enter one value per line, in the format <b>key|label</b> where <em>key</em> is the CSS class name (without the .), and <em>label</em> is the human readable name of the margin. <br /> <b>Note:</b> These options will be reflected on the range slider at the Layout Builder form, so make sure to sort them from lowest to greatest.</p>'),
];
$form['spacing']['margin_group'] = [
'#type' => 'container',
'#title' => $this
->t('Margin'),
'#title_display' => 'invisible',
'#tree' => FALSE,
'#attributes' => [
'class' => [
'bs-admin-d-lg-flex',
'bs-admin-group-form-item-lg-ml',
],
],
];
$form['spacing']['margin_group']['margin'] = [
'#type' => 'textarea',
'#default_value' => $config
->get('margin'),
'#title' => $this
->t('Margin (classes)'),
'#cols' => 60,
'#rows' => 5,
];
for ($i = 0; $i < 4; $i++) {
$form['spacing']['margin_group']['margin_' . $directions[$i]] = [
'#type' => 'textarea',
'#default_value' => $config
->get('margin_' . $directions[$i]),
'#title' => $this
->t('Margin @direction (classes)', [
'@direction' => $directions[$i],
]),
'#cols' => 60,
'#rows' => 5,
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->config()
->set('margin', $form_state
->getValue('margin'))
->set('margin_left', $form_state
->getValue('margin_left'))
->set('margin_top', $form_state
->getValue('margin_top'))
->set('margin_right', $form_state
->getValue('margin_right'))
->set('margin_bottom', $form_state
->getValue('margin_bottom'))
->save();
}
/**
* {@inheritdoc}
*/
public function buildStyleFormElements(array &$form, FormStateInterface $form_state, $storage) {
$directions = [
'left',
'top',
'right',
'bottom',
];
// This only for frontend no storage needed for this field.
$form['margin_type'] = [
'#type' => 'radios',
'#options' => [
'margin' => $this
->t('Margin') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
->t('All') . '</div>',
'margin_left' => $this
->t('Left') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
->t('Left') . '</div>',
'margin_top' => $this
->t('Top') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
->t('Top') . '</div>',
'margin_right' => $this
->t('Right') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
->t('Right') . '</div>',
'margin_bottom' => $this
->t('Bottom') . '<div class="bs_tooltip" data-placement="top" role="tooltip">' . $this
->t('Bottom') . '</div>',
],
'#title' => $this
->t('margin type'),
'#title_display' => 'invisible',
'#default_value' => 'margin',
'#validated' => TRUE,
'#attributes' => [
'class' => [
'bs_col--full',
'bs_input-boxes',
'bs_input-boxes--box-model',
'bs_margin--type',
],
],
'#disable_live_preview' => TRUE,
];
$default_value = 0;
if (isset($storage['margin']['class'])) {
$default_value = $this
->getStyleOptionIndexByClass('margin', $storage['margin']['class']);
}
$form['margin'] = [
'#type' => 'range',
'#title' => $this
->t('Margin'),
'#min' => 0,
'#max' => $this
->getStyleOptionsCount('margin'),
'#step' => 1,
'#default_value' => $default_value,
'#attributes' => [
'class' => [
'bs-field-margin',
],
],
'#states' => [
'visible' => [
':input.bs_margin--type' => [
'value' => 'margin',
],
],
],
];
// Loop through the directions.
for ($i = 0; $i < 4; $i++) {
$default_value = 0;
if (isset($storage['margin_' . $directions[$i]]['class'])) {
$default_value = $this
->getStyleOptionIndexByClass('margin_' . $directions[$i], $storage['margin_' . $directions[$i]]['class']);
}
$form['margin_' . $directions[$i]] = [
'#type' => 'range',
'#title' => $this
->t('Margin @direction', [
'@direction' => $directions[$i],
]),
'#min' => 0,
'#max' => $this
->getStyleOptionsCount('margin_' . $directions[$i]),
'#step' => 1,
'#default_value' => $default_value,
'#attributes' => [
'class' => [
'bs-field-margin-' . $directions[$i],
],
],
'#states' => [
'visible' => [
':input.bs_margin--type' => [
'value' => 'margin_' . $directions[$i],
],
],
],
];
}
// Pass margin options to drupal settings.
$margin_options = [];
$margin_options['margin'] = array_keys($this
->getStyleOptions('margin'));
for ($i = 0; $i < 4; $i++) {
$margin_options['margin_' . $directions[$i]] = array_keys($this
->getStyleOptions('margin_' . $directions[$i]));
}
$form['#attached']['drupalSettings']['bootstrap_styles']['spacing']['margin_classes_options'] = $margin_options;
// Attach the Layout Builder form style for this plugin.
$form['#attached']['library'][] = 'bootstrap_styles/plugin.margin.layout_builder_form';
return $form;
}
/**
* {@inheritdoc}
*/
public function submitStyleFormElements(array $group_elements) {
$directions = [
'left',
'top',
'right',
'bottom',
];
$schema = [
'margin' => [
'class' => $this
->getStyleOptionClassByIndex('margin', $group_elements['margin']),
],
];
for ($i = 0; $i < 4; $i++) {
$schema['margin_' . $directions[$i]]['class'] = $this
->getStyleOptionClassByIndex('margin_' . $directions[$i], $group_elements['margin_' . $directions[$i]]);
}
return $schema;
}
/**
* {@inheritdoc}
*/
public function build(array $build, array $storage, $theme_wrapper = NULL) {
$classes = [];
$directions = [
'left',
'top',
'right',
'bottom',
];
if (isset($storage['margin']['class'])) {
$classes[] = $storage['margin']['class'];
}
for ($i = 0; $i < 4; $i++) {
if (isset($storage['margin_' . $directions[$i]]['class'])) {
$classes[] = $storage['margin_' . $directions[$i]]['class'];
}
}
// Add the classes to the build.
$build = $this
->addClassesToBuild($build, $classes, $theme_wrapper);
// Attach bs-classes to the build.
$build['#attached']['library'][] = 'bootstrap_styles/plugin.margin.build';
return $build;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Margin:: |
public | function |
Overrides StylePluginBase:: |
|
Margin:: |
public | function |
Overrides StylePluginBase:: |
|
Margin:: |
public | function |
Overrides StylePluginBase:: |
|
Margin:: |
public | function |
Overrides StylePluginBase:: |
|
Margin:: |
public | function |
Overrides StylePluginBase:: |
|
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:: |
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:: |
2 |
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. | 4 |
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. | |
StylePluginBase:: |
protected | property | The config factory. | |
StylePluginBase:: |
public | function | Helper function to add the classes to the build. | |
StylePluginBase:: |
public | function | ||
StylePluginBase:: |
constant | Config settings. | ||
StylePluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
StylePluginBase:: |
public | function | Helper function to get the class from the options list. | |
StylePluginBase:: |
public | function | Helper function to get the index of the class at options list. | |
StylePluginBase:: |
public | function | Helper function to get the options of given style name. | |
StylePluginBase:: |
public | function | Helper function to get the options of given style name. | |
StylePluginBase:: |
public | function | Helper function to get SVG Markup. | |
StylePluginBase:: |
public | function |
Return the name of the Styles Group form plugin. Overrides StylePluginInterface:: |
|
StylePluginBase:: |
public | function | ||
StylePluginBase:: |
public | function |
Constructs a StylePluginBase object. Overrides PluginBase:: |
1 |