From 804f0d20fd2f584e4c7fe10a3d1c46f1d1a8d08e Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Tue, 15 Jun 2021 21:09:25 -0400
Subject: [PATCH] ISTWCMS-4704: updating to get only nodes or teasers or both
 in preprocess and removing special code for contacts and using default code

---
 uw_fdsu_theme_resp.theme | 94 ++++++++--------------------------------
 1 file changed, 19 insertions(+), 75 deletions(-)

diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme
index 24a34abe..db055955 100644
--- a/uw_fdsu_theme_resp.theme
+++ b/uw_fdsu_theme_resp.theme
@@ -347,23 +347,37 @@ function uw_fdsu_theme_resp_preprocess_node(&$variables) {
     'uw_ct_news_item',
     'uw_ct_web_page',
     'uw_ct_catalog_item',
+    'uw_ct_contact',
+  ];
+
+  // Teaser to be preprocessed.
+  $teasers_to_preprocess = [
+    'uw_ct_blog',
+    'uw_ct_event',
+    'uw_ct_news_item',
+    'uw_ct_web_page',
+    'uw_ct_catalog_item',
+    'uw_ct_contact',
   ];
 
   // If there is a node that needs preprocessing,
   // set the appropriate variables.
-  if (in_array($variables['node']->getType(), $nodes_to_preprocess)) {
+  if (in_array($variables['node']->getType(), $nodes_to_preprocess) ||
+    in_array($variables['node']->getType(), $teasers_to_preprocess)) {
 
     // The UW service object.
     $uwService = \Drupal::service('uw_cfg_common.uw_service');
 
     // If on a teaser page get the variables for teaser.
-    if ($variables['view_mode'] == 'teaser') {
+    if ($variables['view_mode'] == 'teaser' &&
+      in_array($variables['node']->getType(), $teasers_to_preprocess)) {
       $variables['teaser'] = $uwService->uwGetNodeContent($variables['node'], 'teaser', 'all');
       $variables['rep_date'] = $variables['content']['field_uw_event_date'];
     }
 
     // If on a node page get the variables for now.
-    if ($variables['view_mode'] == 'full') {
+    if ($variables['view_mode'] == 'full' &&
+      in_array($variables['node']->getType(), $nodes_to_preprocess)) {
       $variables['node_data'] = $uwService->uwGetNodeContent($variables['node'], 'full', 'all');
       $variables['node_data']['content'] = $variables['content'];
     }
@@ -372,78 +386,6 @@ function uw_fdsu_theme_resp_preprocess_node(&$variables) {
     // a second print of all the content.
     unset($variables['content']);
   }
-
-  if ($variables['node']->getType() == 'uw_ct_contact') {
-
-    // The UW service object.
-    $uwService = \Drupal::service('uw_cfg_common.uw_service');
-
-    // Get the node.
-    $node = $variables['node'];
-
-    // Get the media id.
-    $mid = $node->field_uw_ct_contact_image->getValue();
-
-    // If there is an image, process it.
-    if ($mid) {
-
-      // Load in the media item.
-      $media = Media::load($mid[0]['target_id']);
-
-      // Get the file id from the media object.
-      $fid = $media->getSource()->getSourceFieldValue($media);
-
-      // If there is a file id, then get the uri,
-      // using the thumbnail image style.
-      if ($fid) {
-        $file = File::load($fid);
-        $image_uri = ImageStyle::load('thumbnail')->buildUrl($file->getFileUri());
-      }
-    }
-
-    // Get the profile.
-    $profile = $node->field_uw_ct_contact_link_profile->getValue();
-
-    // Get personal website or cv.
-    $personal_website = $node->field_uw_ct_contact_link_persona->getValue();
-
-    // Get the additional info field.
-    $additional_info = $node->field_uw_ct_contact_info->getValue();
-
-    // If there is additional info, setup render array.
-    if ($additional_info) {
-      $additional_info = [
-        '#type' => 'processed_text',
-        '#text' => $additional_info[0]['value'],
-        '#format' => $additional_info[0]['format'],
-      ];
-    }
-
-    // Setup the variables for contact.
-    $variables['contact'] = [
-      'title' => $node->getTitle(),
-      'position' => $node->field_uw_ct_contact_title->value,
-      'image' => isset($image_uri) ? $image_uri : NULL,
-      'email' => $node->field_uw_ct_contact_email->value,
-      'phone' => $node->field_uw_ct_contact_phone->value,
-      'location' => $node->field_uw_ct_contact_location->value,
-      'link_profile' => [
-        'uri' => $profile ? $profile[0]['uri'] : NULL,
-        'title' => $profile ? $profile[0]['title'] : NULL,
-      ],
-      'contact_for' => $node->field_uw_ct_contact_contact_for->value,
-      'personal_website' => [
-        'uri' => $personal_website ? $personal_website[0]['uri'] : NULL,
-        'title' => $personal_website ? $personal_website[0]['title'] : NULL,
-      ],
-      'groups' => $uwService->uwGetTermsFromEntityField($node->field_uw_ct_contact_group, 'tags'),
-      'additional_info' => $additional_info ? $additional_info : NULL,
-    ];
-
-    // Unset the content variable so that we do not
-    // get a double print.
-    unset($variables['content']);
-  }
 }
 
 /**
@@ -525,6 +467,7 @@ function uw_fdsu_theme_resp_preprocess_container(&$variables) {
         'uw_ct_blog',
         'uw_ct_news_item',
         'uw_ct_event',
+        'uw_ct_contact',
       ];
 
       // Get the node object.
@@ -545,6 +488,7 @@ function uw_fdsu_theme_resp_preprocess_container(&$variables) {
         if ($node) {
           $variables['header_data'] = $uwService->uwGetNodeContent($node, 'teaser', 'header');
           $variables['footer_data'] = $uwService->uwGetNodeContent($node, 'teaser', 'footer');
+          $test = '';
         }
       }
     }
-- 
GitLab