View source
<?php
namespace Drupal\gridstack\Layout;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
trait GridStackLayoutTrait {
protected $currentUser;
protected $manager;
public static function prepareRegions($optionset = NULL, $clean = TRUE) {
$grids = $optionset
->getEndBreakpointGrids();
$regions = [];
foreach ($grids as $delta => $grid) {
$label_index = $delta + 1;
$label = 'GridStack ' . $label_index;
$regions['gridstack_' . $delta]['label'] = new TranslatableMarkup('@label', [
'@label' => $label,
]);
$nested_grids = $optionset
->getNestedGridsByDelta($delta);
$is_nested = array_filter($nested_grids);
if (!empty($is_nested)) {
if ($clean) {
unset($regions['gridstack_' . $delta]);
}
foreach ($nested_grids as $nested_delta => $nested_grid) {
$label_index_nested = $nested_delta + 1;
$label = 'GridStack ' . $label_index . ':' . $label_index_nested;
$regions['gridstack_' . $delta . '_' . $nested_delta]['label'] = new TranslatableMarkup('@label', [
'@label' => $label,
]);
}
}
}
return $regions;
}
public function defaultConfiguration() {
return [
'regions' => [],
'attributes' => '',
'skin' => '',
'wrapper' => 'div',
'wrapper_classes' => '',
] + parent::defaultConfiguration();
}
public function buildFormElements(array $settings = []) {
$wrapper_options = [
'div' => 'Div',
'article' => 'Article',
'aside' => 'Aside',
'figure' => 'Figure',
'header' => 'Header',
'main' => 'Main',
'footer' => 'Footer',
'section' => 'Section',
];
$elements['wrapper'] = [
'#type' => 'select',
'#options' => $wrapper_options,
'#title' => $this
->t('Wrapper'),
'#default_value' => isset($settings['wrapper']) ? $settings['wrapper'] : 'div',
];
$elements['wrapper_classes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Classes'),
'#description' => $this
->t('E.g.: text-left text-lowercase no-gutters'),
'#default_value' => isset($settings['wrapper_classes']) ? $settings['wrapper_classes'] : '',
];
$elements['attributes'] = [
'#type' => 'textfield',
'#title' => $this
->t('Attributes'),
'#description' => $this
->t('E.g.: role|navigation,data-something|some value'),
'#default_value' => isset($settings['attributes']) ? strip_tags($settings['attributes']) : '',
'#weight' => 20,
];
return $elements;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$regions = $form_state
->getValue('regions');
$settings = $form_state
->getValue('settings');
if (!empty($settings)) {
foreach ($settings as $key => $value) {
$this->configuration[$key] = trim(strip_tags($value));
}
unset($this->configuration['settings']);
}
if (!empty($regions)) {
$stored_regions = [];
foreach ($regions as $name => $info) {
$region = $form_state
->getValue([
'regions',
$name,
]);
foreach ($region as $key => $value) {
$stored_regions[$name][$key] = trim(strip_tags($value));
if (empty($value)) {
unset($stored_regions[$name][$key]);
}
}
}
$this->configuration['regions'] = $stored_regions;
}
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
}