From 052603f8b17997577417997262dab2a189bb14b4 Mon Sep 17 00:00:00 2001
From: ebremner <ebremner@uwaterloo.ca>
Date: Tue, 20 Mar 2018 10:40:41 -0400
Subject: [PATCH] ISTWCMS-2443: Adding preprocessing for responsive image
 styles in paragraphs

---
 uw_fdsu_theme_resp.theme | 52 +++++++++++++++++++++++++++++++++++++++-
 1 file changed, 51 insertions(+), 1 deletion(-)

diff --git a/uw_fdsu_theme_resp.theme b/uw_fdsu_theme_resp.theme
index 63ad9588..62acde74 100644
--- a/uw_fdsu_theme_resp.theme
+++ b/uw_fdsu_theme_resp.theme
@@ -4,7 +4,6 @@
  * uw_fdsu_theme_resp.module
  */
 
-use Drupal\Core\Template\Attribute;
 
 /**
  * Implements hook_preprocess_region().
@@ -26,3 +25,54 @@ function uw_fdsu_theme_resp_preprocess_region(&$variables) {
     $variables['attributes']['class'][] = 'default';
   }
 }
+
+/**
+ * Implements hook_preprocess_paragraph().
+ *
+ * Get the sources for responsive image.
+ */
+function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
+
+  // Check that we are on a specific paragraph and
+  // set image variables accordingly.
+  switch ($variables['paragraph']->getType()) {
+
+    case "uw_para_marketing_block":
+
+      // Set the responsive image variables.
+      _uw_fdsu_theme_resp_add_responsive_image_variables($variables, 'field_uw_marketing_image', 'uw_resp_is_marketing_item');
+
+      break;
+  }
+}
+
+/**
+ * Helper function.
+ *
+ * Set the variables required for a responsive image.
+ *
+ * @param $variables
+ * @param $field_name
+ * @param $responsive_image_style
+ */
+function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field_name, $responsive_image_style) {
+
+  // Get the field entity from the paragraph.
+  $field = $variables['paragraph']->get($field_name)->first();
+
+  // Set the file entity based on the field name.
+  $file = $field->entity;
+
+  // If there is a file present, set responsive image variables.
+  if ($file) {
+
+    // Set uri and image style id.
+    $variables['uri'] = $file->getFileUri();
+    $variables['responsive_image_style_id'] = $responsive_image_style;
+
+    // Call template function from responsive image core module.
+    // It sets variables for srcset, media, type and img_element
+    // for the responsive image style.
+    template_preprocess_responsive_image($variables);
+  }
+}
-- 
GitLab