From 973222bf2985d682c35186d57e63462401e9c904 Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Tue, 8 Jun 2021 13:06:27 -0400
Subject: [PATCH] ISTWCMS-4619: refactoring get node content to work with all
 view modes

---
 src/Service/UWService.php          | 74 ++++++++++++++++--------------
 src/Service/UWServiceInterface.php | 24 +++++-----
 2 files changed, 53 insertions(+), 45 deletions(-)

diff --git a/src/Service/UWService.php b/src/Service/UWService.php
index 9dcefa5e..61476cc1 100644
--- a/src/Service/UWService.php
+++ b/src/Service/UWService.php
@@ -105,7 +105,7 @@ class UWService implements UWServiceInterface {
   /**
    * {@inheritDoc}
    */
-  public function getTeaserContent(Node $node, string $teaser_type, string $teaser_content = 'all'): array {
+  public function uwGetNodeContent(Node $node, string $content_type, string $view_mode, string $content = 'all'): array {
 
     // Flags for getting teaser content.
     $get_header = FALSE;
@@ -114,24 +114,27 @@ class UWService implements UWServiceInterface {
     $get_content = FALSE;
 
     // Setup flags based on teaser content argument.
-    if ($teaser_content == 'all') {
+    if ($content == 'all') {
       $get_header = TRUE;
       $get_footer = TRUE;
-      $get_image = TRUE;
       $get_content = TRUE;
+
+      if ($view_mode == 'teaser') {
+        $get_image = TRUE;
+      }
     }
     else {
-      if ($teaser_content == 'header') {
+      if ($content == 'header') {
         $get_header = TRUE;
       }
 
-      if ($teaser_content == 'footer') {
+      if ($content == 'footer') {
         $get_footer = TRUE;
       }
     }
 
     // Setup the teaser data array, based on flags.
-    switch ($teaser_type) {
+    switch ($content_type) {
 
       case 'blog':
 
@@ -141,7 +144,7 @@ class UWService implements UWServiceInterface {
           'field_uw_audience',
         ];
 
-        $teaser_data = [
+        $content_data = [
           'title' => $get_header ? TRUE : NULL,
           'url' => TRUE,
           'date' => $get_header ? 'field_uw_blog_date' : NULL,
@@ -161,7 +164,7 @@ class UWService implements UWServiceInterface {
           'field_uw_event_type',
         ];
 
-        $teaser_data = [
+        $content_data = [
           'title' => $get_header ? TRUE : NULL,
           'url' => TRUE,
           'date' => $get_header ? 'field_uw_event_date' : NULL,
@@ -179,7 +182,7 @@ class UWService implements UWServiceInterface {
           'field_uw_audience',
         ];
 
-        $teaser_data = [
+        $content_data = [
           'title' => $get_header ? TRUE : NULL,
           'url' => TRUE,
           'date' => $get_header ? 'field_uw_news_date' : NULL,
@@ -190,22 +193,22 @@ class UWService implements UWServiceInterface {
         break;
     }
 
-    return $this->getTeaserData($node, $teaser_type, $teaser_data);
+    return $this->uwGetNodeData($node, $content_type, $view_mode, $content_data);
   }
 
   /**
    * {@inheritDoc}
    */
-  public function getTeaserData(Node $node, string $teaser_type, array $teaser_data): array {
+  public function uwGetNodeData(Node $node, string $content_type, string $view_mode, array $content_data): array {
 
     // Array to store the teaser data, need blank
     // array in case there is no data to return.
-    $teaser = [];
+    $node_data = [];
 
     // Step through each of the teaser data, and if
     // we are to get the data then set the variable
     // inside the teaser array.
-    foreach ($teaser_data as $index => $data) {
+    foreach ($content_data as $index => $data) {
 
       // If there is data to get, then get it.
       if ($data) {
@@ -216,24 +219,25 @@ class UWService implements UWServiceInterface {
         switch ($index) {
 
           case 'title':
-            $teaser['title'] = $node->getTitle();
+            $node_data['title'] = $node->getTitle();
             break;
 
           case 'author':
-            $teaser['author'] = $this->uwGetAuthor($node);
+            $node_data['author'] = $this->uwGetAuthor($node);
             break;
 
           case 'date':
-            if ($teaser_type !== 'event') {
-              $teaser['date'] = date('l, F j, Y', strtotime($node->$data->value));
-            }
-            else {
+            if ($view_mode == 'teaser') {
+              if ($content_type !== 'events') {
+                $node_data['date'] = date('l, F j, Y', strtotime($node->$data->value));
+              } else {
 
-              // Get all the dates.
-              // @todo figure out what date to display for events.
-              $dates = $node->$data->getValue();
+                // Get all the dates.
+                // @todo figure out what date to display for events.
+                $dates = $node->$data->getValue();
 
-              $teaser['date']  = date('l, F j, Y', $dates[0]['value']);
+                $node_data['date'] = date('l, F j, Y', $dates[0]['value']);
+              }
             }
             break;
 
@@ -251,18 +255,20 @@ class UWService implements UWServiceInterface {
             }
 
             if (isset($sources['responsive_sources'])) {
-              $teaser['image']['sources'] = $sources['sources'];
-              $teaser['image']['img_element'] = $sources['img_element']['#uri'];
-              $teaser['image']['alt'] = $sources['alt'];
+              $node_data['image']['sources'] = $sources['sources'];
+              $node_data['image']['img_element'] = $sources['img_element']['#uri'];
+              $node_data['image']['alt'] = $sources['alt'];
             }
             break;
 
           case 'content':
-            $teaser['content'] = [
-              '#type' => 'processed_text',
-              '#text' => $node->$data->value,
-              '#format' => $node->$data->format,
-            ];
+            if ($view_mode == 'teaser') {
+              $node_data['content'] = [
+                '#type' => 'processed_text',
+                '#text' => $node->$data->value,
+                '#format' => $node->$data->format,
+              ];
+            }
             break;
 
           case 'tags':
@@ -270,17 +276,17 @@ class UWService implements UWServiceInterface {
             foreach ($data as $field) {
               $tags = array_merge($tags, $this->uwGetTermsFromEntityField($node->$field, 'tags'));
             }
-            $teaser['tags'] = [$tags];
+            $node_data['tags'] = [$tags];
             break;
 
           case 'url':
-            $teaser['url'] = $node->toUrl()->toString();
+            $node_data['url'] = $node->toUrl()->toString();
             break;
         }
       }
     }
 
-    return $teaser;
+    return $node_data;
   }
 
   /**
diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php
index dbfc43e2..f9e313c7 100644
--- a/src/Service/UWServiceInterface.php
+++ b/src/Service/UWServiceInterface.php
@@ -34,32 +34,34 @@ interface UWServiceInterface {
    *
    * @param \Drupal\node\NodeInterface $node
    *   Node entity.
-   * @param array $variables_to_get
-   *   List of variables to return.
-   * @param string $teaser_type
-   *   Teaser type.
-   * @param string $teaser_content
+   * @param string $content_type
+   *   The content type (i.e. news, event, blog, etc).
+   * @param string $view_mode
+   *   The view mode (i.e. node, teaser, etc).
+   * @param string $content
    *   The type of content to get, values are all, header or footer.
    *
    * @return array
    *   Array of variables and their values.
    */
-  public function getTeaserContent(Node $node, string $teaser_type, string $teaser_content = 'all'): array;
+  public function uwGetNodeContent(Node $node, string $content_type, string $view_mode, string $content = 'all'): array;
 
   /**
    * Gets teaser data.
    *
    * @param \Drupal\node\NodeInterface $node
    *   Node entity.
-   * @param string $teaser_type
-   *   The type of teaser (i.e. blog, events, news, etc).
-   * @param array $teaser_data
+   * @param string $content_type
+   *   The content type (i.e. news, event, blog, etc).
+   * @param string $view_mode
+   *   The view mode (i.e. node, teaser, etc).
+   * @param array $content_data
    *   An array of all the data to get for the teaser.
    *
    * @return array
-   *   Array of teaser values.
+   *   Array of node values.
    */
-  public function getTeaserData(Node $node, string $teaser_type, array $teaser_data): array;
+  public function uwGetNodeData(Node $node, string $content_type, string $view_mode, array $content_data): array;
 
   /**
    * A function to get or check the attached sidebar.
-- 
GitLab