summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2014-09-12 16:58:23 +0100
committerAlex Pott2014-09-12 16:58:23 +0100
commit6d8e333e95a07a157c46a595e6ed0bbba3f1970a (patch)
tree9ab89aff63a47b2cd73bde4f8ba10412b7dd6511
parentb29ad6692474014f69475a31c4fa95ab916b3bde (diff)
Issue #999338 by lauriii, joelpittet, Sir-Arturio, mgifford, mlncn, alarcombe, sun | Eric_A: Fixed theme_image() alt attribute cannot be passed in $variables['attributes'] .
-rw-r--r--core/includes/theme.inc5
-rw-r--r--core/modules/image/src/Tests/ImageThemeFunctionTest.php56
2 files changed, 61 insertions, 0 deletions
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index cb6122a..6ec4ef9 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 97c5a2b..8835839 100644
--- a/core/modules/image/src/Tests/ImageThemeFunctionTest.php
+++ b/core/modules/image/src/Tests/ImageThemeFunctionTest.php
@@ -152,4 +152,60 @@ class ImageThemeFunctionTest extends WebTestBase {
$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.');
+ }
}