MultiWidthLayoutBase.php in Drupal 10
File
core/modules/layout_builder/src/Plugin/Layout/MultiWidthLayoutBase.php
View source
<?php
namespace Drupal\layout_builder\Plugin\Layout;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Plugin\PluginFormInterface;
abstract class MultiWidthLayoutBase extends LayoutDefault implements PluginFormInterface {
public function defaultConfiguration() {
$configuration = parent::defaultConfiguration();
return $configuration + [
'column_widths' => $this
->getDefaultWidth(),
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['column_widths'] = [
'#type' => 'select',
'#title' => $this
->t('Column widths'),
'#default_value' => $this->configuration['column_widths'],
'#options' => $this
->getWidthOptions(),
'#description' => $this
->t('Choose the column widths for this layout.'),
];
return parent::buildConfigurationForm($form, $form_state);
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['column_widths'] = $form_state
->getValue('column_widths');
}
public function build(array $regions) {
$build = parent::build($regions);
$build['#attributes']['class'] = [
'layout',
$this
->getPluginDefinition()
->getTemplate(),
$this
->getPluginDefinition()
->getTemplate() . '--' . $this->configuration['column_widths'],
];
return $build;
}
protected abstract function getWidthOptions();
protected function getDefaultWidth() {
$width_classes = array_keys($this
->getWidthOptions());
return array_shift($width_classes);
}
}