From bc36cf0bfe9ce0bd47b71f076705a2077de8db16 Mon Sep 17 00:00:00 2001
From: Eric Bremner <ebremner@uwaterloo.ca>
Date: Wed, 17 Nov 2021 03:48:07 +0000
Subject: [PATCH] ISTWCMS-5128: adding services to node theming

---
 src/Service/UWService.php        |  1 +
 src/Service/UwNodeContent.php    | 61 ++++++++++++++++++++++++++++++++
 src/Service/UwNodeFieldValue.php | 58 ++++++++++++++++++++++++++++--
 3 files changed, 117 insertions(+), 3 deletions(-)

diff --git a/src/Service/UWService.php b/src/Service/UWService.php
index de7600a2..f379597f 100644
--- a/src/Service/UWService.php
+++ b/src/Service/UWService.php
@@ -136,6 +136,7 @@ class UWService implements UWServiceInterface {
           'uw_ct_news_item',
           'uw_ct_profile',
           'uw_ct_web_page',
+          'uw_ct_service',
         ];
         break;
 
diff --git a/src/Service/UwNodeContent.php b/src/Service/UwNodeContent.php
index 5db20635..a8385efe 100644
--- a/src/Service/UwNodeContent.php
+++ b/src/Service/UwNodeContent.php
@@ -78,6 +78,10 @@ class UwNodeContent {
       case 'uw_ct_profile':
         $content_data = $this->getProfileContent($node_flags);
         break;
+
+      case 'uw_ct_service':
+        $content_data = $this->getServiceContent($node_flags);
+        break;
     }
 
     return $this->uwNodeData->getNodeData($node, $view_mode, $content_data);
@@ -515,6 +519,63 @@ class UwNodeContent {
     return $content_data;
   }
 
+  /**
+   * Get the node content for service content type.
+   *
+   * @param array $node_flags
+   *   The flags for the node.
+   *
+   * @return array
+   *   Array of content to get from the node.
+   */
+  public function getServiceContent(array $node_flags): array {
+
+    // Get the content data.
+    $content_data = $this->setupContentData($node_flags);
+
+    if ($node_flags['get_header']) {
+      $content_data['header']['status'] = $this->addToContentData('select', 'field_uw_service_status');
+    }
+
+    // Setup the actual content.
+    if ($node_flags['get_content']) {
+      $content_data['content'] = $this->addToContentData('content', 'field_uw_service_summary');
+    }
+
+    // Get the footer for the profile.
+    if ($node_flags['get_footer']) {
+      $content_data['footer']['service_information']['has_children'] = TRUE;
+      $content_data['footer']['service_information']['status'] = $this->addToContentData('select', 'field_uw_service_status');
+      $content_data['footer']['service_information']['categories'] = $this->addToContentData('terms', ['field_uw_service_category']);
+
+      $content_data['footer']['service_details']['has_children'] = TRUE;
+      $content_data['footer']['service_details']['popularity'] = $this->addToContentData('plain_text', 'field_uw_service_popularity');
+      $content_data['footer']['service_details']['use_service'] = $this->addToContentData(
+        'terms',
+        [
+          'field_uw_service_audience',
+        ]
+      );
+      $content_data['footer']['service_details']['whats_available'] = $this->addToContentData('multiple_plain_text', 'field_uw_service_available');
+      $content_data['footer']['service_details']['request_service'] = $this->addToContentData('formatted_text', 'field_uw_service_request');
+      $content_data['footer']['service_details']['minimum_notice'] = $this->addToContentData('plain_text', 'field_uw_service_notice');
+      $content_data['footer']['service_details']['average_length'] = $this->addToContentData('plain_text', 'field_uw_service_length');
+      $content_data['footer']['service_details']['pricing_cost'] = $this->addToContentData('formatted_text', 'field_uw_service_cost');
+      $content_data['footer']['service_details']['support'] = $this->addToContentData('formatted_text', 'field_uw_service_support');
+
+      $content_data['footer']['service_hours']['has_children'] = TRUE;
+      $content_data['footer']['service_hours']['hours'] = $this->addToContentData('hours', 'field_uw_service_hours');
+
+      $content_data['footer']['location_info'] = [
+        'has_children' => TRUE,
+        'address' => $this->addToContentData('address', 'field_uw_service_location', 'Location address'),
+        'map' => $this->addToContentData('map', 'field_uw_service_location_coord', 'Location coordinates'),
+      ];
+    }
+
+    return $content_data;
+  }
+
   /**
    * Gets the most recent date.
    *
diff --git a/src/Service/UwNodeFieldValue.php b/src/Service/UwNodeFieldValue.php
index 6905ed93..ae9aeff3 100644
--- a/src/Service/UwNodeFieldValue.php
+++ b/src/Service/UwNodeFieldValue.php
@@ -96,7 +96,13 @@ class UwNodeFieldValue {
 
     // Office hours field type.
     if ($type == 'hours') {
-      return $node->$field_name->view();
+      $hours = $node->$field_name->view();
+      if (isset($hours['#title'])) {
+        return $hours;
+      }
+      else {
+        return [];
+      }
     }
 
     // Date field type (smart dates).
@@ -104,6 +110,11 @@ class UwNodeFieldValue {
       return $this->getDates($node, $field_name, $view_mode);
     }
 
+    // Select list field.
+    if ($type == 'select') {
+      return $node->$field_name->value;
+    }
+
     // Taxonomy terms field type.
     if ($type == 'terms') {
       return $this->getTermsField($node, $field_name);
@@ -125,8 +136,13 @@ class UwNodeFieldValue {
     }
 
     // Plain text field type (textbox).
-    if ($type == 'plain_text') {
-      return $node->$field_name->value;
+    if ($type == 'plain_text' || $type == 'multiple_plain_text') {
+      if ($type == 'multiple_plain_text') {
+        return $this->getPlainText($node, $field_name, TRUE);
+      }
+      else {
+        return $this->getPlainText($node, $field_name);
+      }
     }
 
     // Source or hero image field type.
@@ -230,6 +246,42 @@ class UwNodeFieldValue {
     return $tags;
   }
 
+  /**
+   * Gets a plain text field or fields.
+   *
+   * @param Node $node
+   *   The node.
+   * @param string $field_name
+   *   The field name.
+   * @param bool $multiple
+   *   Flag if multiple plain text.
+   *
+   * @return mixed
+   *   Plain text values.
+   */
+  public function getPlainText(Node $node, string $field_name, bool $multiple = FALSE) {
+
+    // If there are no multiple plain text, just return value.
+    // If there are multiple plain text, step through and
+    // get the values.
+    if (!$multiple) {
+      return $node->$field_name->value;
+    }
+    else {
+
+      // Get the values of the plain text field.
+      $values = $node->$field_name->getValue();
+
+      // Step through each and get the actual value.
+      foreach ($values as $value) {
+        $plain_text[] = $value['value'];
+      }
+
+      // Return array of plain text.
+      return $plain_text;
+    }
+  }
+
   /**
    * {@inheritDoc}
    */
-- 
GitLab