diff --git a/core/modules/responsive_image/responsive_image.module b/core/modules/responsive_image/responsive_image.module index 244e904..b540557 100644 --- a/core/modules/responsive_image/responsive_image.module +++ b/core/modules/responsive_image/responsive_image.module @@ -255,17 +255,13 @@ function theme_responsive_image($variables) { $output[] = ''; $output = array_merge($output, array_map('drupal_render', $sources)); $output[] = ''; - // Output the fallback image. - $dimensions = responsive_image_get_image_dimensions($variables['style_name'], array('width' => $variables['width'], 'height' => $variables['height'])); - - // Use srcset in the fallback image to avoid unnecessary preloading of - // images in older browsers. + // Output the fallback image. Use srcset in the fallback image to avoid + // unnecessary preloading of images in older browsers. $fallback_image = array( '#theme' => 'image', '#srcset' => array( array( 'uri' => _responsive_image_image_style_url($variables['style_name'], $image->getSource()), - 'width' => $dimensions['width'] . 'w', ), ), ); diff --git a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php index 0070a05..9ddf6fa 100644 --- a/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php @@ -126,7 +126,7 @@ public function viewElements(FieldItemListInterface $items) { $elements = array(); // Check if the formatter involves a link. if ($this->getSetting('image_link') == 'content') { - $uri = $items->getEntity()->urlInfo(); + $uri = $items->getEntity()->urlInfo()->toArray(); // @todo Remove when theme_responsive_image_formatter() has support for route name. $uri['path'] = $items->getEntity()->getSystemPath(); } diff --git a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php index 5e58613..e6a4b1b 100644 --- a/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php +++ b/core/modules/responsive_image/src/Tests/ResponsiveImageFieldDisplayTest.php @@ -95,6 +95,65 @@ public function testResponsiveImageFieldFormattersPrivate() { } /** + * Test responsive image formatters on node display linked to the file. + */ + public function testResponsiveImageFieldFormattersLinkToFile() { + $this->_testResponsiveImageFieldFormattersLink('file'); + } + + /** + * Test responsive image formatters on node display linked to the node. + */ + public function testResponsiveImageFieldFormattersLinkToNode() { + $this->_testResponsiveImageFieldFormattersLink('node'); + } + + /** + * Test responsive image formatters linked to the file or node. + */ + public function _testResponsiveImageFieldFormattersLink($link_type) { + $field_name = drupal_strtolower($this->randomMachineName()); + $this->createImageField($field_name, 'article', array('uri_scheme' => 'public')); + // Create a new node with an image attached. + $test_image = current($this->drupalGetTestFiles('image')); + $nid = $this->uploadNodeImage($test_image, $field_name, 'article'); + $node = node_load($nid, TRUE); + + // Use the responsive image formatter linked to file formatter. + $display_options = array( + 'type' => 'responsive_image', + 'module' => 'responsive_image', + 'settings' => array( + 'image_link' => $link_type, + 'responsive_image_mapping' => 'mapping_one', + 'fallback_image_style' => 'large', + ), + ); + entity_get_display('node', 'article', 'default') + ->setComponent($field_name, $display_options) + ->save(); + + // Create a derivative so at least one MIME type will be known. + $large_style = ImageStyle::load('large'); + $image_uri = file_load($node->{$field_name}->target_id)->getFileUri(); + $large_style->createDerivative($image_uri, $large_style->buildUri($image_uri)); + + // Output should contain all image styles and all breakpoints. + $this->drupalGet('node/' . $nid); + switch ($link_type) { + case 'file': + // Make sure the link to the file is present. + $this->assertPattern('/assertPattern('/url() . '"(.*?)>get($image_uri); - $dimensions = array('width' => $image->getWidth(), 'height' => $image->getHeight()); - $large_style->transformDimensions($dimensions); $fallback_image = array( '#theme' => 'image', '#srcset' => array( array( 'uri' => $large_style->buildUrl($image->getSource()), - 'width' => $dimensions['width'] . 'w', ), ), );