laces_base-1.0.x-dev/src/Plugin/Layout/TwoColumn.php
src/Plugin/Layout/TwoColumn.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 TwoColumn extends LayoutDefault implements PluginFormInterface {
/**
* {@inheritdoc}
*/
public function defaultConfiguration(): array {
$configuration = parent::defaultConfiguration();
return $configuration + [
'container_type' => 'Default',
'column_widths' => '50%/50%',
];
}
/**
* {@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' => [
'50-50' => '50%/50%',
'33-67' => '33%/67%',
'67-33' => '67%/33%',
'25-75' => '25%/75%',
'75-25' => '75%/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 '50-50':
$build['first']['#attributes']['class'][] = 'col';
$build['second']['#attributes']['class'][] = 'col';
break;
case '33-67';
$build['first']['#attributes']['class'][] = 'col-4';
$build['second']['#attributes']['class'][] = 'col-8';
break;
case '67-33';
$build['first']['#attributes']['class'][] = 'col-8';
$build['second']['#attributes']['class'][] = 'col-4';
break;
case '25-75';
$build['first']['#attributes']['class'][] = 'col-3';
$build['second']['#attributes']['class'][] = 'col-9';
break;
case '75-25';
$build['first']['#attributes']['class'][] = 'col-9';
$build['second']['#attributes']['class'][] = 'col-3';
break;
}
return $build;
}
}
