From 7e6f4548390c4d221a260946b7af9631101407c9 Mon Sep 17 00:00:00 2001
From: Eric Bremner <ebremner@uwaterloo.ca>
Date: Wed, 19 Oct 2022 15:25:37 -0400
Subject: [PATCH] ISTWCMS-5880: adding function to get the media flags

---
 src/Service/UWService.php          | 40 ++++++++++++++++++++++++++++++
 src/Service/UWServiceInterface.php | 11 ++++++++
 src/Service/UwNodeFieldValue.php   |  1 +
 3 files changed, 52 insertions(+)

diff --git a/src/Service/UWService.php b/src/Service/UWService.php
index b63cfeaf..850b7781 100644
--- a/src/Service/UWService.php
+++ b/src/Service/UWService.php
@@ -247,6 +247,46 @@ class UWService implements UWServiceInterface {
     return $preprocess;
   }
 
+  /**
+   * {@inheritDoc}
+   */
+  public function uwGetMediaFlags(Node $node): array {
+
+    // Set the defaults for the flags to false.
+    $media_flags['has_media'] = FALSE;
+    $media_flags['show_header_in_media'] = FALSE;
+
+    // If there is a type of media field, continue to look
+    // for media flags.
+    if ($node->hasField('field_uw_type_of_media')) {
+
+      // If there is a type of media set, then set has_media,
+      // and look for media in header.
+      if ($type_of_media = $node->field_uw_type_of_media->value) {
+
+        // Set has_media flag.
+        $media_flags['has_media'] = TRUE;
+
+        // If the type of media is image, the header needs to
+        // go inside the media, so set the flag.
+        if ($type_of_media == 'image') {
+          $media_flags['show_header_in_media'] = TRUE;
+        }
+      }
+      // Special case is contact, we need to look if there
+      // is a portrait image and if so set the has_media.
+      else if ($node->getType() == 'uw_ct_contact') {
+
+        // If there is a portrait image, set the has_media.
+        if ($node->field_uw_ct_contact_image->getValue()) {
+          $media_flags['has_media'] = TRUE;
+        }
+      }
+    }
+
+    return $media_flags;
+  }
+
   /**
    * {@inheritDoc}
    */
diff --git a/src/Service/UWServiceInterface.php b/src/Service/UWServiceInterface.php
index b8eaee27..a9fe706c 100644
--- a/src/Service/UWServiceInterface.php
+++ b/src/Service/UWServiceInterface.php
@@ -59,6 +59,17 @@ interface UWServiceInterface {
    */
   public function uwGetNodePreprocessing(string $type): array;
 
+  /**
+   * Gets the flags for showing media on nodes.
+   *
+   * @param \Drupal\node\Node $node
+   *   Node entity.
+   *
+   * @return array
+   *   Array of flags for media.
+   */
+  public function uwGetMediaFlags(Node $node): array;
+
   /**
    * Gets dates from node.
    *
diff --git a/src/Service/UwNodeFieldValue.php b/src/Service/UwNodeFieldValue.php
index bb9a247a..6ea5bbc4 100644
--- a/src/Service/UwNodeFieldValue.php
+++ b/src/Service/UwNodeFieldValue.php
@@ -317,6 +317,7 @@ class UwNodeFieldValue {
       'style' => $node->field_uw_text_overlay_style->value,
       'transition_speed' => $node->field_uw_transition_speed->value,
       'media_width' => $media_width_classes ?? NULL,
+      'bundle' => $node->getType(),
     ];
 
     return $banners;
-- 
GitLab