Skip to content
Snippets Groups Projects
UwTwoColumnLayout.php 2.08 KiB
<?php

namespace Drupal\uw_cfg_common\Plugin\Layout;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Layout\LayoutDefault;
use Drupal\Core\Plugin\PluginFormInterface;

/**
 * A UW two column layout.
 *
 * @Layout(
 *   id = "uw_two_column",
 *   label = @Translation("Two columns"),
 *   category = @Translation("UW layouts"),
 *   template = "templates/layout/layout--uwtwocol",
 *   regions = {
 *     "first" = {
 *       "label" = @Translation("Column 1"),
 *     },
 *     "second" = {
 *       "label" = @Translation("Column 2"),
 *     },
 *   }
 * )
 */
class UwTwoColumnLayout extends LayoutDefault implements PluginFormInterface {

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {

    // Get the config for this layout.
    $configuration = $this->getConfiguration();

    // The options for the column widths.
    $options = [
      'even-split' => $this->t('Even split (50%, 50%)'),
      'larger-left' => $this->t('Larger left (67%, 33%)'),
      'larger-right' => $this->t('Larger right (33%, 67%)'),
    ];

    // The form element for the column widths.
    $form['layout_settings']['column_class'] = [
      '#type' => 'select',
      '#title' => $this->t('Column widths'),
      '#default_value' => !empty($configuration['column_class']) ? $configuration['column_class'] : 'even-split',
      '#options' => $options,
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {

    // Set the column class in the config.
    $this->configuration['column_class'] = $form_state->getValue(['layout_settings', 'column_class'], NULL);
  }

  /**
   * {@inheritdoc}
   */
  public function build(array $regions) {

    // Build the render array as usual.
    $build = parent::build($regions);

    // Retrieve the config for the layout.
    $configuration = $this->getConfiguration();

    // Set the column class to be used in the layout template.
    $build['#settings']['column_class'] = $configuration['column_class'];

    return $build;
  }

}