From 93a05065107fb9d8d7053f04a63417e14e0bcfac Mon Sep 17 00:00:00 2001
From: Dylan Donkersgoed <ddonkersgoed@echidna.ca>
Date: Mon, 19 Nov 2018 12:52:05 -0500
Subject: [PATCH] [3000614-2] 3000614-2

---
 fullcalendar_view.theme.inc                    | 90 +++++++++++++++-----------
 src/Plugin/views/style/FullCalendarDisplay.php |  8 +++
 2 files changed, 62 insertions(+), 36 deletions(-)

diff --git a/fullcalendar_view.theme.inc b/fullcalendar_view.theme.inc
index 5972b47..287126b 100644
--- a/fullcalendar_view.theme.inc
+++ b/fullcalendar_view.theme.inc
@@ -91,11 +91,11 @@ function template_preprocess_views_view_fullcalendar(array &$variables) {
       // Result entity of current row.
       $current_entity = $row->_entity;
       // Start field is vital, if it doesn't exist then ignore this entity.
-      if (!$current_entity->hasField($start_field)) {
+      if ($options['use_entity_fields'] && !$current_entity->hasField($start_field)) {
         continue;
       }
       // Monthly recurring.
-      if ($current_entity->hasField('field_monthly_event')) {
+      if ($options['use_entity_fields'] && $current_entity->hasField('field_monthly_event')) {
         $monthly = $current_entity->get('field_monthly_event')->getValue();
       }
       // If the monthly recurring is set, the weekly recurring will be ignored.
@@ -108,7 +108,7 @@ function template_preprocess_views_view_fullcalendar(array &$variables) {
         }
       }
       // Weekly recurring.
-      elseif ($current_entity->hasField('field_weekly_event')) {
+      elseif ($options['use_entity_fields'] && $current_entity->hasField('field_weekly_event')) {
         $weekly = $current_entity->get('field_weekly_event')->getValue();
       }
       if (!empty($weekly)) {
@@ -133,18 +133,32 @@ function template_preprocess_views_view_fullcalendar(array &$variables) {
         // Event type.
         $event_type = $current_entity->get($tax_field)->target_id;
       }
-      // Calendar event start date.
-      $start_date = $current_entity->get($start_field)->getValue();
-      // Calendar event end date.
-      $end_date = empty($end_field) || !$current_entity->hasField($end_field) ? '' : $current_entity->get($end_field)->getValue();
-      // Description for events.
-      $des = empty($des_field) || !$current_entity->hasField($des_field) ? '' : $current_entity->get($des_field)->getValue();
-      if (is_array($des)) {
-        $des = reset($des);
-        if (isset($des['value'])) {
-          $des = $des['value'];
+
+      if ($options['use_entity_fields']) {
+        // Calendar event start date.
+        $start_date = $current_entity->get($start_field)->getValue();
+        // Calendar event end date.
+        $end_date = empty($end_field) || !$current_entity->hasField($end_field) ? '' : $current_entity->get($end_field)->getValue();
+        // Description for events.
+        $des = empty($des_field) || !$current_entity->hasField($des_field) ? '' : $current_entity->get($des_field)->getValue();
+        if (is_array($des)) {
+          $des = reset($des);
+          if (isset($des['value'])) {
+            $des = $des['value'];
+          }
         }
       }
+      else {
+        // Calendar event start date.
+        $start_date = $fields[$start_field]->advancedRender($row);
+        // Calendar event end date.
+        $end_date_raw = empty($end_field) ? '' : $fields[$end_field]->advancedRender($row);
+        $end_date = empty($end_date_raw) ? '' : $end_date_raw;
+        // Description for events.
+        $des_raw = empty($des_field) ? '' : $fields[$des_field]->advancedRender($row);
+        $des = empty($des_raw) ? '' : $des_raw;
+      }
+
       // Event title.
       $title = $current_entity->label();
       $entry = [
@@ -157,15 +171,17 @@ function template_preprocess_views_view_fullcalendar(array &$variables) {
       if (!empty($start_date)) {
         // There might be more than one value for a field,
         // but we only need the first one and ignore others.
-        $start_date = reset($start_date)['value'];
-        // Examine the field type.
-        if ($start_field_option['type'] === 'timestamp') {
-          $start_date = intval($start_date);
-          $start_date = date(DATE_ATOM, $start_date);
-        }
-        elseif (strpos($start_field_option['type'], 'datetime') === FALSE && strpos($start_field_option['type'], 'daterange') === FALSE) {
-          // This field is not a valid date time field.
-          continue;
+        if ($options['use_entity_fields']) {
+          $start_date = reset($start_date)['value'];
+          // Examine the field type.
+          if ($start_field_option['type'] === 'timestamp') {
+            $start_date = intval($start_date);
+            $start_date = date(DATE_ATOM, $start_date);
+          }
+          elseif (strpos($start_field_option['type'], 'datetime') === FALSE && strpos($start_field_option['type'], 'daterange') === FALSE) {
+            // This field is not a valid date time field.
+            continue;
+          }
         }
         // A user who doesn't have the permission can't edit an event.
         if (!$current_entity->access('update')) {
@@ -213,20 +229,22 @@ function template_preprocess_views_view_fullcalendar(array &$variables) {
       // Cope with end date in the same way as start date above.
       if (!empty($end_date)) {
 
-        if ($end_field_option['type'] === 'timestamp') {
-          $end_date = reset($end_date)['value'];
-          $end_date = intval($end_date);
-          $end_date = date(DATE_ATOM, $end_date);
-        }
-        elseif (strpos($end_field_option['type'], 'daterange') !== FALSE) {
-          $end_date = $end_date[0]['end_value'];
-        }
-        elseif (strpos($end_field_option['type'], 'datetime') === FALSE) {
-          // This field is not a valid date time field.
-          $end_date = '';
-        }
-        else {
-          $end_date = reset($end_date)['value'];
+        if ($options['use_entity_fields']) {
+          if ($end_field_option['type'] === 'timestamp') {
+            $end_date = reset($end_date)['value'];
+            $end_date = intval($end_date);
+            $end_date = date(DATE_ATOM, $end_date);
+          }
+          elseif (strpos($end_field_option['type'], 'daterange') !== FALSE) {
+            $end_date = $end_date[0]['end_value'];
+          }
+          elseif (strpos($end_field_option['type'], 'datetime') === FALSE) {
+            // This field is not a valid date time field.
+            $end_date = '';
+          }
+          else {
+            $end_date = reset($end_date)['value'];
+          }
         }
 
         if (!empty($end_date)) {
diff --git a/src/Plugin/views/style/FullCalendarDisplay.php b/src/Plugin/views/style/FullCalendarDisplay.php
index 13760b3..75ad15a 100644
--- a/src/Plugin/views/style/FullCalendarDisplay.php
+++ b/src/Plugin/views/style/FullCalendarDisplay.php
@@ -66,6 +66,7 @@ class FullCalendarDisplay extends StylePluginBase {
     $options['start'] = ['default' => ''];
     $options['end'] = ['default' => ''];
     $options['des'] = ['default' => ''];
+    $options['use_entity_fields'] = ['default' => TRUE];
     $options['business_start'] = ['default' => ''];
     $options['business_end'] = ['default' => ''];
     $options['content_type'] = ['default' => ''];
@@ -138,6 +139,13 @@ class FullCalendarDisplay extends StylePluginBase {
       '#title' => t('Display'),
       '#description' => t('Calendar display settings.'),
     ];
+    $form['use_entity_fields'] = [
+      '#type' => 'checkbox',
+      '#title' => $this->t('Use entity fields'),
+      '#description' => $this->t('Use entity fields for rendering rather than the raw view fields. This is checked by default to prevent legacy views from breaking. However, it will prevent using views rewriting and does not support non-standard date fields (fields other than timestamp, datetime and daterange).'),
+      '#fieldset' => 'display',
+      '#default_value' => $this->options['use_entity_fields'],
+    ];
     // Right side buttons.
     $form['right_buttons'] = [
       '#type' => 'checkboxes',
-- 
2.14.1

