laces_base-1.0.x-dev/src/Plugin/Layout/ThreeColumn.php
src/Plugin/Layout/ThreeColumn.php
<?php
namespace Drupal\laces_base\Plugin\Layout;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Plugin\PluginFormInterface;
/**
* Configurable two column layout plugin class.
*
* @internal
* Plugin classes are internal.
*/
class ThreeColumn extends LayoutDefault implements PluginFormInterface {
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
$configuration = parent::defaultConfiguration();
return $configuration + [
'container_type' => 'Default',
'column_widths' => '25%/50%/25%',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
$configuration = $this->getConfiguration();
$form['container_type'] = [
'#type' => 'select',
'#title' => $this->t('Container breakpoint'),
'#default_value' => $configuration['container_type'],
'#options' => [
'container' => 'Default',
'container-sm' => 'Small',
'container-md' => 'Medium',
'container-lg' => 'Large',
'container-xl' => 'Extra large',
'container-xxl' => 'Extra extra large',
'container-fluid' => 'Fluid',
'container-edge' => 'Edge to edge',
],
'#description' => $this->t('Choose the container breakpoint for this layout.'),
];
$form['column_widths'] = [
'#type' => 'select',
'#title' => $this->t('Column Widths'),
'#default_value' => $configuration['column_widths'],
'#options' => [
'25-50-25' => '25%/50%/25%',
'33-34-33' => '33%/34%/33%',
'25-25-50' => '25%/25%/50%',
'50-25-25' => '50%/25%/25%',
],
'#description' => $this->t('Choose the column widths for the layout.'),
];
return parent::buildConfigurationForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
// any additional form validation that is required
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['container_type'] = $form_state->getValue('container_type');
$this->configuration['column_widths'] = $form_state->getValue('column_widths');
}
/**
* {@inheritdoc}
*/
public function build(array $regions): array {
$build = parent::build($regions);
$build['#attributes']['class'][] = $this->configuration['container_type'];
$build['#row_attributes']['class'][] = 'row';
if ($this->configuration['container_type'] == 'container-edge') {
$build['#row_attributes']['class'][] = 'g-0';
}
switch ($this->configuration['column_widths']) {
case '25-50-25':
$build['first']['#attributes']['class'][] = 'col-3';
$build['second']['#attributes']['class'][] = 'col-6';
$build['third']['#attributes']['class'][] = 'col-3';
break;
case '33-34-33';
$build['first']['#attributes']['class'][] = 'col';
$build['second']['#attributes']['class'][] = 'col';
$build['third']['#attributes']['class'][] = 'col';
break;
case '25-25-50';
$build['first']['#attributes']['class'][] = 'col-3';
$build['second']['#attributes']['class'][] = 'col-3';
$build['third']['#attributes']['class'][] = 'col';
break;
case '50-25-25';
$build['first']['#attributes']['class'][] = 'col';
$build['second']['#attributes']['class'][] = 'col-3';
$build['third']['#attributes']['class'][] = 'col-3';
break;
}
return $build;
}
}
