diff --git a/core/includes/theme.inc b/core/includes/theme.inc index f136ca1e1685c25c4b2b8cd00cc283ce3abe2e6c..d10ce95d9d05289c29c755bbd80f06cdfd329e46 100644 --- a/core/includes/theme.inc +++ b/core/includes/theme.inc @@ -2263,7 +2263,7 @@ function drupal_common_theme() { // - http://dev.w3.org/html5/spec/Overview.html#alt // The title attribute is optional in all cases, so it is omitted by // default. - 'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), 'sizes' => NULL, 'srcset' => array()), + 'variables' => array('uri' => NULL, 'width' => NULL, 'height' => NULL, 'alt' => '', 'title' => NULL, 'attributes' => array(), 'sizes' => NULL, 'srcset' => array(), 'style_name' => NULL), 'template' => 'image', ), 'breadcrumb' => array( diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 29839d2eea7f42c267c4e27ce1d5f995dbeffbfb..de32196d5f31a80996e4046de4d0573260a74873 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -281,15 +281,13 @@ function template_preprocess_image_style(&$variables) { $style->transformDimensions($dimensions); - // Add in the image style name as an HTML class. - $variables['attributes']['class'][] = 'image-style-' . drupal_html_class($variables['style_name']); - $variables['image'] = array( '#theme' => 'image', '#width' => $dimensions['width'], '#height' => $dimensions['height'], '#attributes' => $variables['attributes'], '#uri' => $style->buildUrl($variables['uri']), + '#style_name' => $variables['style_name'], ); if (isset($variables['alt']) || array_key_exists('alt', $variables)) { diff --git a/core/modules/image/src/Tests/ImageDimensionsTest.php b/core/modules/image/src/Tests/ImageDimensionsTest.php index 3d787d98803da2764cd7aafe2d26249990f8ab16..4d6c76b37533de7d200d3b217cdfef72b0299eb5 100644 --- a/core/modules/image/src/Tests/ImageDimensionsTest.php +++ b/core/modules/image/src/Tests/ImageDimensionsTest.php @@ -67,7 +67,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="120" height="60" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="120" height="60" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -88,7 +88,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="60" height="120" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="60" height="120" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -110,7 +110,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -132,7 +132,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -150,7 +150,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="45" height="90" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -171,7 +171,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -191,7 +191,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" width="30" height="30" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" width="30" height="30" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -212,7 +212,7 @@ function testImageDimensions() { $effect_id = $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); $this->drupalGet($url); $this->assertResponse(200, 'Image was generated at the URL.'); @@ -231,7 +231,7 @@ function testImageDimensions() { $style->addImageEffect($effect); $style->save(); - $this->assertEqual($this->getImageTag($variables), '<img class="image-style-test" src="' . $url . '" alt="" />'); + $this->assertEqual($this->getImageTag($variables), '<img src="' . $url . '" alt="" class="image-style-test" />'); } /** diff --git a/core/modules/system/templates/image.html.twig b/core/modules/system/templates/image.html.twig index b6b238a355166dbf52ed4a99443f277264a3f09b..2f29304d48ad1ea69e282d209ec6e17b1c000d38 100644 --- a/core/modules/system/templates/image.html.twig +++ b/core/modules/system/templates/image.html.twig @@ -5,10 +5,16 @@ * * Available variables: * - attributes: HTML attributes for the img tag. + * - style_name: (optional) The name of the image style applied. * * @see template_preprocess_image() * * @ingroup themeable */ #} -<img{{ attributes }} /> +{% +set classes = [ + style_name ? 'image-style-' ~ style_name|clean_class, +] +%} +<img{{ attributes.addClass(classes) }} />