ClassyLayout.php in Layout Section Classes 8
File
src/ClassyLayout.php
View source
<?php
namespace Drupal\layout_section_classes;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Plugin\PluginFormInterface;
class ClassyLayout extends LayoutDefault implements PluginFormInterface {
public function build(array $regions) {
$build = parent::build($regions);
$classes = $this->configuration['additional']['classes'] ?? [];
$build['#attributes']['class'] = $build['#attributes']['class'] ?? [];
foreach ($classes as $class_set) {
if (is_string($class_set) && $class_set) {
$build['#attributes']['class'][] = $class_set;
}
if (is_array($class_set)) {
$build['#attributes']['class'] = array_merge($build['#attributes']['class'], array_filter($class_set));
}
}
return $build;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['classes'] = [
'#type' => 'container',
'#tree' => TRUE,
];
$plugin_configuration = $this->configuration['additional']['classes'] ?? [];
foreach ($this
->getPluginDefinition()
->get('classes') as $key => $class_definition) {
if (!is_array($class_definition['options']) || empty($class_definition['options'])) {
throw new \Exception('The "options" key is required for layout class definitions.');
}
$definition_default = $class_definition['default'] ?? NULL;
$form['classes'][$key] = [
'#title' => $class_definition['label'] ?? $this
->t('Classes'),
'#type' => 'select',
'#multiple' => $class_definition['multiple'] ?? FALSE,
'#options' => $class_definition['options'],
'#required' => $class_definition['required'] ?? FALSE,
'#default_value' => $plugin_configuration[$key] ?? $definition_default,
'#description' => $class_definition['description'] ?? '',
];
if (!$form['classes'][$key]['#required'] || $form['classes'][$key]['#required'] && $form['classes'][$key]['#default_value'] === NULL) {
$form['classes'][$key]['#empty_option'] = $this
->t('- Select -');
}
}
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['additional']['classes'] = $form_state
->getValue('classes');
}
}
Classes
Name |
Description |
ClassyLayout |
A layout plugin class for layouts with selectable classes. |