Skip to content
Snippets Groups Projects
Commit ebc50453 authored by Liam Morland's avatar Liam Morland Committed by Liam Morland
Browse files

ISTWCMS-2452: Return alt text separately from sources in...

ISTWCMS-2452: Return alt text separately from sources in _uw_fdsu_theme_resp_add_responsive_image_variables()

Return an array with keys 'sources' and 'alt'.
parent 622d2e38
No related branches found
No related tags found
No related merge requests found
......@@ -66,8 +66,7 @@ function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
$field = $variables['paragraph']->get('field_uw_marketing_image')->first();
// Set the responsive image variables.
$sources = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item');
$variables['sources'] = $sources;
$variables['sources'] = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item')['sources'];
$display_title = _uw_fdsu_theme_resp_get_field_value_from_paragraph($variables['paragraph'], 'field_uw_display_title');
......@@ -89,8 +88,7 @@ function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
// Get the field entity from the paragraph.
$field = $variables['paragraph']->get('field_uw_sph_header_image')->first();
$sources = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_sph_header_image');
$variables['sources'] = $sources;
$variables['sources'] = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_sph_header_image')['sources'];
break;
......@@ -161,7 +159,7 @@ function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
// Set the required variables for the options block.
$options[$i]['alt'] = $sources['alt'];
$options[$i]['sources'] = $sources;
$options[$i]['sources'] = $sources['sources'];
$options[$i]['responsive_image_style_id'] = 'uw_resp_is_sph_header_image';
$options[$i]['option_item_id'] = 'option_item_id_' . $i;
}
......@@ -182,8 +180,7 @@ function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
$field = $variables['paragraph']->get('field_uw_image_block_image')->first();
// Set the responsive image variables.
$sources = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item');
$variables['sources'] = $sources;
$variables['sources'] = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item')['sources'];
break;
......@@ -219,7 +216,7 @@ function uw_fdsu_theme_resp_preprocess_paragraph(&$variables) {
// If there is a field to process, process it.
if (isset($field)) {
// Get the sources for the responsive image.
$quicklinks[$counter]['sources'] = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item');
$quicklinks[$counter]['sources'] = _uw_fdsu_theme_resp_add_responsive_image_variables($variables, $field, 'uw_resp_is_marketing_item')['sources'];
}
// Set the link section title.
......@@ -275,13 +272,14 @@ function _uw_fdsu_theme_resp_get_field_value_from_paragraph($paragraph, $field)
* @param string $responsive_image_style
* The ID of the responsive image style.
*
* @return array
* An array of image sources.
* @return array|null
* An array with keys:
* - sources: An array of image sources.
* - alt: The alternative text.
*/
function _uw_fdsu_theme_resp_add_responsive_image_variables(array &$variables, $field, $responsive_image_style) {
function _uw_fdsu_theme_resp_add_responsive_image_variables(array &$variables, Drupal\image\Plugin\Field\FieldType\ImageItem $field, string $responsive_image_style) : ?array {
// If there is a file present, set responsive image variables.
if ($file = $field->entity) {
// Set uri and image style id.
$variables['uri'] = $file->getFileUri();
$variables['responsive_image_style_id'] = $responsive_image_style;
......@@ -291,33 +289,22 @@ function _uw_fdsu_theme_resp_add_responsive_image_variables(array &$variables, $
// for the responsive image style.
template_preprocess_responsive_image($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'];
// Step through each source and get string values.
foreach ($old_sources as $source) {
// Set new_sources to the string values.
$new_sources[] = [
$sources = [];
foreach ($variables['sources'] as $source) {
$sources[] = [
'srcset' => $source['srcset']->value(),
'media' => $source['media']->value(),
'type' => $source['type']->value(),
];
}
// 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;
return [
'sources' => $sources,
'alt' => $field->get('alt')->getValue(),
];
}
return NULL;
}
/**
......
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