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

Fixing getting responsive image variables to any field

parent ebdd3c71
No related branches found
No related tags found
No related merge requests found
......@@ -134,22 +134,13 @@ function _uw_fdsu_theme_resp_get_field_value_from_paragraph($paragraph, $field)
* Set the variables required for a responsive image.
*
* @param $variables
* @param $field_name
* @param $field
* @param $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();
// Set the alt tag from the field entity.
$variables['alt'] = $field->get('alt')->getValue();
// Set the file entity based on the field name.
$file = $field->entity;
function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field, $responsive_image_style) {
// If there is a file present, set responsive image variables.
if ($file) {
if ($file = $field->entity) {
// Set uri and image style id.
$variables['uri'] = $file->getFileUri();
......@@ -160,30 +151,31 @@ function _uw_fdsu_theme_resp_add_responsive_image_variables(&$variables, $field_
// for the responsive image style.
template_preprocess_responsive_image($variables);
// Set the old_sources to the sources in variables.
// Start with clean arrays.
unset($old_sources);
unset($new_sources);
// Set old_sources to the ones set in the template preprocess.
$old_sources = $variables['sources'];
// Unset the sources variable.
unset($variables['sources']);
// Step through each source and get string values.
foreach ($old_sources as $source) {
// 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 (
// Set new_sources to the string values.
$new_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;
}
// Set the alt tag for the picture.
$new_sources['alt'] = $field->get('alt')->getValue();
// Unset the old sources.
unset($variables['sources']);
// Return the new sources.
return $new_sources;
}
}
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