From ebdd3c71381a5edc5f9aa3e56edf8be9568db8e1 Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Wed, 30 May 2018 23:17:39 -0400
Subject: [PATCH] Fixing responsive images to allow for background images

---
 uw_fdsu_theme_resp.theme | 28 +++++++++++++++++++++++++++-
 1 file changed, 27 insertions(+), 1 deletion(-)

diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme
index f3c08daf..01114a54 100644
--- a/uw_fdsu_theme_resp.theme
+++ b/uw_fdsu_theme_resp.theme
@@ -137,7 +137,7 @@ function _uw_fdsu_theme_resp_get_field_value_from_paragraph($paragraph, $field)
  * @param $field_name
  * @param $responsive_image_style
  */
-function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field_name, $responsive_image_style) {
+function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field_name, $responsive_image_style, $use_separate_variable = FALSE) {
 
   // Get the field entity from the paragraph.
   $field = $variables['paragraph']->get($field_name)->first();
@@ -159,5 +159,31 @@ function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field_
     // It sets variables for srcset, media, type and img_element
     // for the responsive image style.
     template_preprocess_responsive_image($variables);
+
+    // Set the old_sources to the sources in variables.
+    $old_sources = $variables['sources'];
+
+    // Unset the sources variable.
+    unset($variables['sources']);
+
+    // Step through each source and only get value.
+    // This is need so that a render doesn't happen
+    // and HTML escapes are put in place.
+    foreach($old_sources as $source) {
+      $sources[] = array (
+        'srcset' => $source['srcset']->value(),
+        'media' => $source['media']->value(),
+        'type' => $source['type']->value(),
+      );
+    }
+
+    // If not to set sources in variables, return sources.
+    // Otherwise set the sources in variables.
+    if ($use_separate_variable) {
+      return $sources;
+    }
+    else {
+      $variables['sources'] = $sources;
+    }
   }
 }
-- 
GitLab