diff --git a/core/includes/theme.inc b/core/includes/theme.inc index cb6122a6aa4d50f1ce5844cea4dc5e5cc778848d..6ec4ef9a7bf56efe3f9fc4dc759869d16898f6e3 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -1122,6 +1122,11 @@ function template_preprocess_image(&$variables) { foreach (array('width', 'height', 'alt', 'title') as $key) { if (isset($variables[$key])) { + // If the property has already been defined in the attributes, + // do not override, including NULL. + if (array_key_exists($key, $variables['attributes'])) { + continue; + } $variables['attributes'][$key] = $variables[$key]; } } diff --git a/core/modules/image/src/Tests/ImageThemeFunctionTest.php b/core/modules/image/src/Tests/ImageThemeFunctionTest.php index 97c5a2b1deb6dce9a2ef359f3a32e39c2414bd2b..88358398ae16698ed7e431d31bf1d0e185488ac9 100644 --- a/core/modules/image/src/Tests/ImageThemeFunctionTest.php +++ b/core/modules/image/src/Tests/ImageThemeFunctionTest.php @@ -152,4 +152,60 @@ function testImageStyleTheme() { $this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.'); } + /** + * Tests image alt attribute functionality. + */ + function testImageAltFunctionality() { + // Test using alt directly with alt attribute. + $image_with_alt_property = array( + '#theme' => 'image', + '#uri' => '/core/themes/bartik/logo.png', + '#alt' => 'Regular alt', + '#title' => 'Test title', + '#width' => '50%', + '#height' => '50%', + '#attributes' => array('class' => 'image-with-regular-alt', 'id' => 'my-img'), + ); + + $this->drupalSetContent(drupal_render($image_with_alt_property)); + $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-regular-alt", ":alt" => "Regular alt")); + $this->assertEqual(count($elements), 1, 'Regular alt displays correctly'); + + // Test using alt attribute inside attributes. + $image_with_alt_attribute_alt_attribute = array( + '#theme' => 'image', + '#uri' => '/core/themes/bartik/logo.png', + '#width' => '50%', + '#height' => '50%', + '#attributes' => array( + 'class' => 'image-with-attribute-alt', + 'id' => 'my-img', + 'title' => 'New test title', + 'alt' => 'Attribute alt', + ), + ); + + $this->drupalSetContent(drupal_render($image_with_alt_attribute_alt_attribute)); + $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-attribute-alt", ":alt" => "Attribute alt")); + $this->assertEqual(count($elements), 1, 'Attribute alt displays correctly'); + + // Test using alt attribute as property and inside attributes. + $image_with_alt_attribute_both = array( + '#theme' => 'image', + '#uri' => '/core/themes/bartik/logo.png', + '#width' => '50%', + '#height' => '50%', + '#alt' => 'Kitten sustainable', + '#attributes' => array( + 'class' => 'image-with-attribute-alt', + 'id' => 'my-img', + 'title' => 'New test title', + 'alt' => 'Attribute alt', + ), + ); + + $this->drupalSetContent(drupal_render($image_with_alt_attribute_both)); + $elements = $this->xpath('//img[contains(@class, class) and contains(@alt, :alt)]', array(":class" => "image-with-attribute-alt", ":alt" => "Attribute alt")); + $this->assertEqual(count($elements), 1, 'Attribute alt overrides alt property if both set.'); + } }