From f58a06b72147cd67ecdc129c13352bfbdd35947f Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Fri, 28 Aug 2020 11:22:57 -0400
Subject: [PATCH] ISTWCMS-4053: adding custom layouts

---
 src/Plugin/Layout/UwFourColumnLayout.php  | 90 +++++++++++++++++++++++
 src/Plugin/Layout/UwThreeColumnLayout.php | 87 ++++++++++++++++++++++
 src/Plugin/Layout/UwTwoColumnLayout.php   | 81 ++++++++++++++++++++
 uw_cfg_common.layouts.yml                 | 45 ++++++++++++
 4 files changed, 303 insertions(+)
 create mode 100644 src/Plugin/Layout/UwFourColumnLayout.php
 create mode 100644 src/Plugin/Layout/UwThreeColumnLayout.php
 create mode 100644 src/Plugin/Layout/UwTwoColumnLayout.php
 create mode 100644 uw_cfg_common.layouts.yml

diff --git a/src/Plugin/Layout/UwFourColumnLayout.php b/src/Plugin/Layout/UwFourColumnLayout.php
new file mode 100644
index 00000000..e3a5c00b
--- /dev/null
+++ b/src/Plugin/Layout/UwFourColumnLayout.php
@@ -0,0 +1,90 @@
+<?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 four column layout.
+ *
+ * @Layout(
+ *   id = "uw_four_column",
+ *   label = @Translation("Four columns"),
+ *   category = @Translation("UW layouts"),
+ *   template = "templates/layout/layout--uwfourcol",
+ *   regions = {
+ *     "first" = {
+ *       "label" = @Translation("Column 1"),
+ *     },
+ *     "second" = {
+ *       "label" = @Translation("Column 2"),
+ *     },
+ *     "third" = {
+ *       "label" = @Translation("Column 3"),
+ *     },
+ *     "four" = {
+ *       "label" = @Translation("Column 4"),
+ *     },
+ *   }
+ * )
+ */
+class UwFourColumnLayout 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 (25%, 25%, 25%, 25%)'),
+      'larger-left' => $this->t('Larger left (50%, 16.67%, 16.67%, 16.67%)'),
+      'larger-second' => $this->t('Larger second (16.67%, 50%, 16.67%, 16.67%)'),
+      'larger-third' => $this->t('Larger third (16.67%, 16.67%, 50%, 16.67%)'),
+      'larger-right' => $this->t('Larger right (16.67%, 16.67%, 16.67%, 50%)'),
+      'legacy-23-27-27-23' => $this->t('Legacy (23%, 27%, 27%, 23%)'),
+    ];
+
+    // 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;
+  }
+
+}
diff --git a/src/Plugin/Layout/UwThreeColumnLayout.php b/src/Plugin/Layout/UwThreeColumnLayout.php
new file mode 100644
index 00000000..2590744a
--- /dev/null
+++ b/src/Plugin/Layout/UwThreeColumnLayout.php
@@ -0,0 +1,87 @@
+<?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 three column layout.
+ *
+ * @Layout(
+ *   id = "uw_three_column",
+ *   label = @Translation("Three columns"),
+ *   category = @Translation("UW layouts"),
+ *   template = "templates/layout/layout--uwthreecol",
+ *   regions = {
+ *     "first" = {
+ *       "label" = @Translation("Column 1"),
+ *     },
+ *     "second" = {
+ *       "label" = @Translation("Column 2"),
+ *     },
+ *     "third" = {
+ *       "label" = @Translation("Column 3"),
+ *     },
+ *   }
+ * )
+ */
+class UwThreeColumnLayout 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 (33%, 34%, 33%)'),
+      'larger-left' => $this->t('Larger left (50%, 25%, 25%)'),
+      'larger-middle' => $this->t('Larger middle (25%, 50%, 25%)'),
+      'larger-right' => $this->t('Larger right (25%, 25%, 50%)'),
+      'legacy-38-38-24' => $this->t('Legacy (38%, 38%, 24%)'),
+      'legacy-24-38-38' => $this->t('Legacy (24%, 38%, 38%)'),
+    ];
+
+    // 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;
+  }
+
+}
diff --git a/src/Plugin/Layout/UwTwoColumnLayout.php b/src/Plugin/Layout/UwTwoColumnLayout.php
new file mode 100644
index 00000000..e5897bda
--- /dev/null
+++ b/src/Plugin/Layout/UwTwoColumnLayout.php
@@ -0,0 +1,81 @@
+<?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;
+  }
+
+}
diff --git a/uw_cfg_common.layouts.yml b/uw_cfg_common.layouts.yml
new file mode 100644
index 00000000..cf54fc4f
--- /dev/null
+++ b/uw_cfg_common.layouts.yml
@@ -0,0 +1,45 @@
+uw_two_column:
+  label: 'UW Two columns'
+  category: 'UW layouts'
+  class: '\Drupal\uw_cfg_common\Plugin\Layout\UwTwoColumnLayout'
+  template: uw_fdsu_theme_resp/templates/layout/layout--uwtwocol
+  default_region: first
+  regions:
+    first:
+      label: One
+    second:
+      label: Two
+  icon_map:
+    - [first, second]
+uw_three_column:
+  label: 'UW Three columns'
+  category: 'UW layouts'
+  class: '\Drupal\uw_cfg_common\Plugin\Layout\UwThreeColumnLayout'
+  template: uw_fdsu_theme_resp/templates/layout/layout--uwthreecol
+  default_region: first
+  regions:
+    first:
+      label: One
+    second:
+      label: Two
+    third:
+      label: Third
+  icon_map:
+    - [first, second, third]
+uw_four_column:
+  label: 'UW Four columns'
+  category: 'UW layouts'
+  class: '\Drupal\uw_cfg_common\Plugin\Layout\UwFourColumnLayout'
+  template: uw_fdsu_theme_resp/templates/layout/layout--uwfourcol
+  default_region: first
+  regions:
+    first:
+      label: One
+    second:
+      label: Two
+    third:
+      label: Third
+    fourth:
+      label: Fourth
+  icon_map:
+    - [first, second, third, fourth]
-- 
GitLab