Commit b933a5b4 authored by webchick's avatar webchick

Issue #1939068 by joelpittet, tlattimore, Cottser, chrisjlee, rteijeiro,...

Issue #1939068 by joelpittet, tlattimore, Cottser, chrisjlee, rteijeiro, mdrummond, drupalninja99, jenlampton, shanethehat, InternetDevels, DamienMcKenna, gavin.hughes, carsonevans, ezeedub: Convert theme_image() to Twig.
parent 005e7e43
......@@ -1224,9 +1224,11 @@ function template_preprocess_links(&$variables) {
}
/**
* Returns HTML for an image.
* Prepares variables for image templates.
*
* @param $variables
* Default template: image.html.twig.
*
* @param array $variables
* An associative array containing:
* - uri: Either the path of the image file (relative to base_path()) or a
* full URL.
......@@ -1246,17 +1248,14 @@ function template_preprocess_links(&$variables) {
* popular browsers.
* - attributes: Associative array of attributes to be placed in the img tag.
*/
function theme_image($variables) {
$attributes = $variables['attributes'];
$attributes['src'] = file_create_url($variables['uri']);
function template_preprocess_image(&$variables) {
$variables['attributes']['src'] = file_create_url($variables['uri']);
foreach (array('width', 'height', 'alt', 'title') as $key) {
if (isset($variables[$key])) {
$attributes[$key] = $variables[$key];
$variables['attributes'][$key] = $variables[$key];
}
}
return '<img' . new Attribute($attributes) . ' />';
}
/**
......@@ -2429,6 +2428,7 @@ function drupal_common_theme() {
// 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()),
'template' => 'image',
),
'breadcrumb' => array(
'variables' => array('breadcrumb' => NULL),
......
......@@ -71,7 +71,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="120" height="60" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="120" height="60" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -92,7 +92,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="60" height="120" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="60" height="120" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -114,7 +114,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -136,7 +136,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -154,7 +154,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="45" height="90" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -175,7 +175,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -195,7 +195,7 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="30" height="30" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" width="30" height="30" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -216,7 +216,7 @@ function testImageDimensions() {
$effect_id = $style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
$this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
$this->drupalGet($url);
$this->assertResponse(200, 'Image was generated at the URL.');
......@@ -235,6 +235,6 @@ function testImageDimensions() {
$style->saveImageEffect($effect);
$img_tag = theme_image_style($variables);
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />');
$this->assertEqual($img_tag, '<img class="image-style-test" src="' . $url . '" alt="" />' . "\n");
}
}
......@@ -96,16 +96,16 @@ function testImageFormatterTheme() {
'path' => $path,
),
);
$rendered_element = render($element);
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
$this->drupalSetContent(render($element));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders with a NULL value for the alt option.');
// Test using theme_image_formatter() without an image title, alt text, or
// link options.
$element['#item']->alt = '';
$rendered_element = render($element);
$expected_result = '<a href="' . base_path() . $path . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" alt="" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders without title, alt, or path options.');
$this->drupalSetContent(render($element));
$elements = $this->xpath('//a[@href=:path]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(':path' => base_path() . $path, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders without title, alt, or path options.');
// Link the image to a fragment on the page, and not a full URL.
$fragment = $this->randomName();
......@@ -114,9 +114,9 @@ function testImageFormatterTheme() {
'external' => TRUE,
'fragment' => $fragment,
);
$rendered_element = render($element);
$expected_result = '<a href="#' . $fragment . '"><img class="image-style-test" src="' . $url . '" width="' . $image->getWidth() . '" height="' . $image->getHeight() . '" alt="" /></a>';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.');
$this->drupalSetContent(render($element));
$elements = $this->xpath('//a[@href=:fragment]/img[@class="image-style-test" and @src=:url and @width=:width and @height=:height and @alt=""]', array(':fragment' => '#' . $fragment, ':url' => $url, ':width' => $image->getWidth(), ':height' => $image->getHeight()));
$this->assertEqual(count($elements), 1, 'theme_image_formatter() correctly renders a link fragment.');
}
/**
......@@ -138,15 +138,15 @@ function testImageStyleTheme() {
'#style_name' => 'image_test',
'#uri' => $original_uri,
);
$rendered_element = render($element);
$expected_result = '<img class="image-style-image-test" src="' . $url . '" alt="" />';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_style() renders an image correctly.');
$this->drupalSetContent(render($element));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url and @alt=""]', array(':url' => $url));
$this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly.');
// Test using theme_image_style() with a NULL value for the alt option.
$element['#alt'] = NULL;
$rendered_element = render($element);
$expected_result = '<img class="image-style-image-test" src="' . $url . '" />';
$this->assertEqual($expected_result, $rendered_element, 'theme_image_style() renders an image correctly with a NULL value for the alt option.');
$this->drupalSetContent(render($element));
$elements = $this->xpath('//img[@class="image-style-image-test" and @src=:url]', array(':url' => $url));
$this->assertEqual(count($elements), 1, 'theme_image_style() renders an image correctly with a NULL value for the alt option.');
}
}
{#
/**
* @file
* Default theme implementation of an image.
*
* Available variables:
* - attributes: HTML attributes for the img tag.
*
* @see template_preprocess_image()
*
* @ingroup themeable
*/
#}
<img{{ attributes }} />
......@@ -87,8 +87,9 @@ public function testTourFunctionality() {
));
$this->assertNotEqual(count($elements), 1, 'Did not find Italian variant of tip 1.');
// Ensure that plugin's work.
$this->assertRaw('img src="http://local/image.png"', 'Image plugin tip found.');
// Ensure that plugins work.
$elements = $this->xpath('//img[@src="http://local/image.png"]');
$this->assertEqual(count($elements), 1, 'Image plugin tip found.');
// Navigate to tour-test-2/subpath and verify the tour_test_2 tip is found.
$this->drupalGet('tour-test-2/subpath');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment