From 17a393174658c6dbe9eb6d4e7b4a291523d3359b Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Thu, 17 Jun 2021 01:23:05 -0400
Subject: [PATCH] ISTWCMS-4704: adding logic for featured images on nodes

---
 .../blocks/block--page-title-block.html.twig  | 24 +++++++++++
 templates/node/node.html.twig                 |  9 ++++
 uw_fdsu_theme_resp.theme                      | 43 +++++++++++++++++++
 3 files changed, 76 insertions(+)
 create mode 100644 templates/blocks/block--page-title-block.html.twig

diff --git a/templates/blocks/block--page-title-block.html.twig b/templates/blocks/block--page-title-block.html.twig
new file mode 100644
index 00000000..b879f014
--- /dev/null
+++ b/templates/blocks/block--page-title-block.html.twig
@@ -0,0 +1,24 @@
+{% if not featured_image %}
+  {%
+    set classes = [
+    'block',
+    'block-' ~ configuration.provider|clean_class,
+    'block-' ~ plugin_id|clean_class,
+  ]
+  %}
+
+  <div{{ attributes.addClass(classes) }}>
+    {% if admin_label %}
+      <div class="uw-admin-label">{{ admin_label }}</div>
+    {% endif %}
+    {{ title_prefix }}
+    {% if label %}
+      <h2{{ title_attributes }}>{{ label }}</h2>
+    {% endif %}
+    {{ title_suffix }}
+    {% block content %}
+      {{ content }}
+    {% endblock %}
+  </div>
+{% endif %}
+
diff --git a/templates/node/node.html.twig b/templates/node/node.html.twig
index 59f71c56..d4c0666a 100644
--- a/templates/node/node.html.twig
+++ b/templates/node/node.html.twig
@@ -87,6 +87,14 @@
 
 {{ attach_library('seven/classy.node') }}
 
+{% if view_mode == 'full' %}
+  {% if node_data.sources %}
+    {% set featured_image = 'yes' %}
+  {% else %}
+    {% set featured_image = 'no' %}
+  {% endif %}
+{% endif %}
+
 {% embed '@layouts/node/node.twig' with {
   'classes': classes,
   'has_sidebar': sidebar ? 'Yes' : 'No'
@@ -96,6 +104,7 @@
     <div{{ content_attributes.addClass('node__content') }}>
       {% include '@components/card/card--node/card--node.twig' with {
         'node': node_data,
+        'featured_image': featured_image,
       } %}
     </div>
   {% endblock %}
diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme
index ba1de1a5..801bae98 100644
--- a/uw_fdsu_theme_resp.theme
+++ b/uw_fdsu_theme_resp.theme
@@ -393,6 +393,49 @@ function uw_fdsu_theme_resp_preprocess_node(&$variables) {
  */
 function uw_fdsu_theme_resp_preprocess_block(&$variables) {
 
+  // Look at page title block to see if we have a featured image.
+  // If we do then, set variable to not show page title.
+  if ($variables['plugin_id'] == 'page_title_block') {
+
+    // Set the featured image variable to false, we will only
+    // change if there is a featured image.
+    $variables['featured_image'] = FALSE;
+
+    // Load the node.
+    $node = \Drupal::routeMatch()->getParameter('node');
+
+    // If there is a node, check that it has a featured image.
+    if ($node) {
+
+      // The UW service object.
+      $uwService = \Drupal::service('uw_cfg_common.uw_service');
+
+      // Set the node type.
+      $node_type = $node->getType();
+
+      // Get the list of content types that are allowed to have
+      // feature images from our service.
+      $featured_image = $uwService->uwGetFeaturedImageContentTypes();
+
+      // If node is allowed to have a featured image, make sure that
+      // node actually has an image.
+      if (in_array($node_type, array_keys($featured_image))) {
+
+        // Get the field name.
+        $field_name = $featured_image[$node_type];
+
+        // Get the image object values from the node.
+        $image = $node->$field_name->getValue();
+
+        // If there is an image present, set the variable so that
+        // the page title will not be displayed.
+        if ($image) {
+          $variables['featured_image'] = TRUE;
+        }
+      }
+    }
+  }
+
   // If we are in layout builder (this is set much earlier in
   // the page load process), then continue to look if we need
   // to add the admin_label and css classes.
-- 
GitLab