template_preprocess_field_slideshow() breaks HTML output structure

Created on 2 April 2020, over 5 years ago
Updated 21 July 2025, 10 days ago

Versions

  1. Drupal 7.67
  2. field_slideshow 7.x-2.0-beta2 (11 Dec 2019)


What's wrong?
In template_preprocess_field_slideshow(), there is a specific execution path that can render the HTML output structure incorrectly. Here is an example of the erroneous output:

<div class='inner-slide'>
  <img class="some_classes" src="some_path" alt="some_value" title="some_value2" /></diwidth="480" height />


Why is this happening?
There's a block of code in template_preprocess_field_slideshow() that will guarantee a <div> wrapper around the image.

if (isset($variables['image_style']) && $variables['image_style'] != '') {
  $image['style_name'] = $variables['image_style'];
  $image['breakpoints'] = $variables['breakpoints'];
  $variables['items'][$num]['image'] = "<div class='inner-slide'>" . theme($slide_theme, $image) . "</div>";
} elseif (isset($variables['file_style']) && $variables['file_style'] != '') {
  $media = file_view((object)$variables['items'][$num], $variables['file_style']);
  $variables['items'][$num]['image'] = "<div class='inner-slide'>" . render($media) . "</div>";
} else {
  $variables['items'][$num]['image'] = "<div class='inner-slide'>" . theme('image', $image) . "</div>";
}

There is a total of 3 possible execution paths here. No matter which path is taken, a <div> wrapper is added.

Next, not too far down, we have this block of code:

if ($item["type"] == 'image' && strpos($variables['items'][$num]['image'], 'width=') === FALSE) {
  $variables['items'][$num]['image'] = drupal_substr($variables['items'][$num]['image'], 0, -2) . "width=\"$dimensions[width]\" height=\"$dimensions[height]\" />";
}

This replaces the last 2 characters "v>" of </div> with extra width and height info. And here it is assumed that the last 2 characters are "/>" belonging to the <img> tag - which is not true.

Solution:

if ($item["type"] == 'image' && strpos($variables['items'][$num]['image'], 'width=') === FALSE) {
  // $variables['items'][$num]['image'] = drupal_substr($variables['items'][$num]['image'], 0, -2) . "width=\"$dimensions[width]\" height=\"$dimensions[height]\" />";
  $variables['items'][$num]['image'] = str_replace("<img ", "<img width=\"$dimensions[width]\" height=\"$dimensions[height]\" ", $variables['items'][$num]['image']);
}
πŸ› Bug report
Status

Closed: outdated

Component

Code

Created by

πŸ‡ΈπŸ‡¬Singapore ThePiano.SG

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024