Skip to content
Snippets Groups Projects
Commit 052603f8 authored by Eric Bremner's avatar Eric Bremner
Browse files

ISTWCMS-2443: Adding preprocessing for responsive image styles in paragraphs

parent 364a28a3
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment