From 7646332905fa163d68b4002bde52c82d7cd5617f Mon Sep 17 00:00:00 2001
From: Owen Bush <ojb@ukhhf.co.uk>
Date: Fri, 1 Mar 2019 22:36:28 -0700
Subject: [PATCH] Events now save.

---
 config/schema/recurring_events.schema.yml     | 16 +++++++++++
 .../Field/FieldType/MonthlyRecurringDate.php  |  1 -
 .../Field/FieldType/WeeklyRecurringDate.php   |  3 ---
 .../MonthlyRecurringDateWidget.php            | 19 +++++++++++++
 .../FieldWidget/WeeklyRecurringDateWidget.php | 27 ++++++++++++++++++-
 5 files changed, 61 insertions(+), 5 deletions(-)
 create mode 100644 config/schema/recurring_events.schema.yml

diff --git a/config/schema/recurring_events.schema.yml b/config/schema/recurring_events.schema.yml
new file mode 100644
index 00000000..998d10e8
--- /dev/null
+++ b/config/schema/recurring_events.schema.yml
@@ -0,0 +1,16 @@
+recurring_events.config
+  type: config_object
+  label: 'Recurring Events'
+  mapping:
+    interval:
+      type: integer
+      label: 'The interval between times when creating events'
+    min_time:
+      type: string
+      label: 'The earliest an event can start'
+    max_time:
+      type: string
+      label: 'The latest an event can start'
+    time_format:
+      type: string
+      label: 'The formatting of times when creating events'
\ No newline at end of file
diff --git a/src/Plugin/Field/FieldType/MonthlyRecurringDate.php b/src/Plugin/Field/FieldType/MonthlyRecurringDate.php
index a5935393..45933546 100644
--- a/src/Plugin/Field/FieldType/MonthlyRecurringDate.php
+++ b/src/Plugin/Field/FieldType/MonthlyRecurringDate.php
@@ -26,7 +26,6 @@ class MonthlyRecurringDate extends WeeklyRecurringDate {
 
     $schema['columns']['type'] = [
       'type' => 'varchar',
-      'not null' => TRUE,
       'length' => 20,
     ];
 
diff --git a/src/Plugin/Field/FieldType/WeeklyRecurringDate.php b/src/Plugin/Field/FieldType/WeeklyRecurringDate.php
index 119b7ad9..02d4ec3f 100644
--- a/src/Plugin/Field/FieldType/WeeklyRecurringDate.php
+++ b/src/Plugin/Field/FieldType/WeeklyRecurringDate.php
@@ -27,19 +27,16 @@ class WeeklyRecurringDate extends DateRangeItem {
 
     $schema['columns']['time'] = [
       'type' => 'varchar',
-      'not null' => TRUE,
       'length' => 20,
     ];
 
     $schema['columns']['duration'] = [
       'type' => 'int',
-      'not null' => TRUE,
       'unsigned' => TRUE,
     ];
 
     $schema['columns']['days'] = [
       'type' => 'varchar',
-      'not null' => TRUE,
       'length' => 255,
     ];
 
diff --git a/src/Plugin/Field/FieldWidget/MonthlyRecurringDateWidget.php b/src/Plugin/Field/FieldWidget/MonthlyRecurringDateWidget.php
index 6a8d3372..5c71f46b 100644
--- a/src/Plugin/Field/FieldWidget/MonthlyRecurringDateWidget.php
+++ b/src/Plugin/Field/FieldWidget/MonthlyRecurringDateWidget.php
@@ -93,6 +93,25 @@ class MonthlyRecurringDateWidget extends WeeklyRecurringDateWidget {
     return $element;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
+    $values = parent::massageFormValues($values, $form, $form_state);
+
+    foreach ($values as &$item) {
+
+      $item['day_occurrence'] = array_filter($item['day_occurrence']);
+      if (!empty($item['day_occurrence'])) {
+        $item['day_occurrence'] = implode(',', $item['day_occurrence']);
+      }
+      else {
+        $item['day_occurrence'] = '';
+      }
+    }
+    return $values;
+  }
+
   /**
    * Return day of month options for events.
    *
diff --git a/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php b/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php
index dc1ed4ee..1d15457b 100644
--- a/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php
+++ b/src/Plugin/Field/FieldWidget/WeeklyRecurringDateWidget.php
@@ -76,6 +76,31 @@ class WeeklyRecurringDateWidget extends DateRangeDefaultWidget {
     return $element;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function massageFormValues(array $values, array $form, FormStateInterface $form_state) {
+    $values = parent::massageFormValues($values, $form, $form_state);
+
+    foreach ($values as &$item) {
+      if (empty($item['value'])) {
+        $item['value'] = '';
+      }
+      if (empty($item['end_value'])) {
+        $item['end_value'] = '';
+      }
+
+      $item['days'] = array_filter($item['days']);
+      if (!empty($item['days'])) {
+        $item['days'] = implode(',', $item['days']);
+      }
+      else {
+        $item['days'] = '';
+      }
+    }
+    return $values;
+  }
+
   /**
    * Generate times based on specific intervals and min/max times.
    *
@@ -91,7 +116,7 @@ class WeeklyRecurringDateWidget extends DateRangeDefaultWidget {
     $interval = $config->get('interval') * 60;
     $min_time = $config->get('min_time');
     $max_time = $config->get('max_time');
-    $format = $config->get('format'); */
+    $format = $config->get('time_format'); */
 
     // Take interval in minutes, and multiply it by 60 to convert to seconds.
     $interval = 15 * 60;
-- 
GitLab