diff --git a/modules/image/image.field.inc b/modules/image/image.field.inc index 1be15839ced47e29cdd5d7463bbda552da87cb98..60c0f5ac00ff2e0e3bf20dae85a54020ee68b8b0 100644 --- a/modules/image/image.field.inc +++ b/modules/image/image.field.inc @@ -600,9 +600,12 @@ function theme_image_formatter($variables) { $item = $variables['item']; $image = array( 'path' => $item['uri'], - 'alt' => $item['alt'], ); + if (array_key_exists('alt', $item)) { + $image['alt'] = $item['alt']; + } + if (isset($item['attributes'])) { $image['attributes'] = $item['attributes']; } @@ -613,7 +616,7 @@ function theme_image_formatter($variables) { } // Do not output an empty 'title' attribute. - if (drupal_strlen($item['title']) > 0) { + if (isset($item['title']) && drupal_strlen($item['title']) > 0) { $image['title'] = $item['title']; } @@ -625,9 +628,11 @@ function theme_image_formatter($variables) { $output = theme('image', $image); } - if (!empty($variables['path']['path'])) { + // The link path and link options are both optional, but for the options to be + // processed, the link path must at least be an empty string. + if (isset($variables['path']['path'])) { $path = $variables['path']['path']; - $options = $variables['path']['options']; + $options = isset($variables['path']['options']) ? $variables['path']['options'] : array(); // When displaying an image inside a link, the html option must be TRUE. $options['html'] = TRUE; $output = l($output, $path, $options); diff --git a/modules/image/image.test b/modules/image/image.test index 2a3559981d1b77d0a6ced55fbdbccede63e15c40..1ca8465067364dceec836e6f8080a41ddd1e1981 100644 --- a/modules/image/image.test +++ b/modules/image/image.test @@ -1597,3 +1597,64 @@ class ImageFieldDefaultImagesTestCase extends ImageFieldTestCase { } } + +/** + * Tests image theme functions. + */ +class ImageThemeFunctionWebTestCase extends DrupalWebTestCase { + + public static function getInfo() { + return array( + 'name' => 'Image theme functions', + 'description' => 'Test that the image theme functions work correctly.', + 'group' => 'Image', + ); + } + + function setUp() { + parent::setUp(array('image')); + } + + /** + * Tests usage of the image field formatters. + */ + function testImageFormatterTheme() { + // Create an image. + $files = $this->drupalGetTestFiles('image'); + $file = reset($files); + $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME); + + // Create a style. + image_style_save(array('name' => 'test')); + $url = image_style_url('test', $original_uri); + + // Test using theme_image_formatter() without an image title, alt text, or + // link options. + $path = $this->randomName(); + $element = array( + '#theme' => 'image_formatter', + '#image_style' => 'test', + '#item' => array( + 'uri' => $original_uri, + ), + '#path' => array( + 'path' => $path, + ), + ); + $rendered_element = render($element); + $expected_result = ''; + $this->assertEqual($expected_result, $rendered_element, '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(); + $element['#path']['path'] = ''; + $element['#path']['options'] = array( + 'external' => TRUE, + 'fragment' => $fragment, + ); + $rendered_element = render($element); + $expected_result = ''; + $this->assertEqual($expected_result, $rendered_element, 'theme_image_formatter() correctly renders a link fragment.'); + } + +}