Issue #2348255 by Jelle_S, attiks, mdrummond, Wim Leers, giupenni, jnettik, Lukas von Blarer, SaltPacket: Provide option to use srcset and/or sizes attributes on img tag instead of the picture element
parent
09b1d9bcfc
commit
c63f02a12c
|
|
@ -178,6 +178,24 @@ function template_preprocess_responsive_image(&$variables) {
|
|||
$variables['sources'][] = responsive_image_build_source_attributes($image, $variables, $breakpoints[$breakpoint_id], $multipliers);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($variables['sources']) && count($variables['sources']) === 1 && !isset($variables['sources'][0]['media'])) {
|
||||
// There is only one source tag with an empty media attribute. This means
|
||||
// we can output an image tag with the srcset attribute in stead of a
|
||||
// picture tag.
|
||||
$variables['output_image_tag'] = TRUE;
|
||||
foreach ($variables['sources'][0] as $attribute => $value) {
|
||||
if ($attribute != 'type') {
|
||||
$variables['attributes'][$attribute] = $value;
|
||||
}
|
||||
}
|
||||
$variables['img_element'] = array(
|
||||
'#theme' => 'image',
|
||||
'#uri' => _responsive_image_image_style_url($responsive_image_style->getFallbackImageStyle(), $image->getSource()),
|
||||
);
|
||||
}
|
||||
else {
|
||||
$variables['output_image_tag'] = FALSE;
|
||||
// Prepare the fallback image. Use srcset in the fallback image to avoid
|
||||
// unnecessary preloading of images in older browsers. See
|
||||
// http://scottjehl.github.io/picturefill/#using-picture and
|
||||
|
|
@ -190,6 +208,8 @@ function template_preprocess_responsive_image(&$variables) {
|
|||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
if (isset($variables['attributes'])) {
|
||||
if (isset($variables['attributes']['alt'])) {
|
||||
$variables['img_element']['#alt'] = $variables['attributes']['alt'];
|
||||
|
|
|
|||
|
|
@ -244,6 +244,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
|
|||
$cache_tags_header = $this->drupalGetHeader('X-Drupal-Cache-Tags');
|
||||
$this->assertTrue(!preg_match('/ image_style\:/', $cache_tags_header), 'No image style cache tag found.');
|
||||
|
||||
$this->removeWhiteSpace();
|
||||
$this->assertRaw($default_output, 'Image linked to file formatter displaying correctly on full node view.');
|
||||
// Verify that the image can be downloaded.
|
||||
$this->assertEqual(file_get_contents($test_image->uri), $this->drupalGet(file_create_url($image_uri)), 'File was downloaded successfully.');
|
||||
|
|
@ -398,6 +399,52 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
|
|||
$this->assertPattern('/srcset="' . preg_quote($thumbnail_style->buildUrl($image_uri), '/') . ' 1x".+?media="\(min-width: 0px\)"/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests responsive image formatter on node display with one source.
|
||||
*/
|
||||
public function testResponsiveImageFieldFormattersOneSource() {
|
||||
$this->responsiveImgStyle
|
||||
// Test the output of an empty media query.
|
||||
->addImageStyleMapping('responsive_image_test_module.empty', '1x', array(
|
||||
'image_mapping_type' => 'image_style',
|
||||
'image_mapping' => 'medium',
|
||||
))
|
||||
->addImageStyleMapping('responsive_image_test_module.empty', '2x', array(
|
||||
'image_mapping_type' => 'image_style',
|
||||
'image_mapping' => 'large',
|
||||
))
|
||||
->save();
|
||||
$node_storage = $this->container->get('entity.manager')->getStorage('node');
|
||||
$field_name = Unicode::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', $this->randomMachineName());
|
||||
$node_storage->resetCache(array($nid));
|
||||
|
||||
// Use the responsive image formatter linked to file formatter.
|
||||
$display_options = array(
|
||||
'type' => 'responsive_image',
|
||||
'settings' => array(
|
||||
'image_link' => '',
|
||||
'responsive_image_style' => 'style_one',
|
||||
),
|
||||
);
|
||||
$display = entity_get_display('node', 'article', 'default');
|
||||
$display->setComponent($field_name, $display_options)
|
||||
->save();
|
||||
|
||||
// View the node.
|
||||
$this->drupalGet('node/' . $nid);
|
||||
|
||||
// Assert the media attribute is present if it has a value.
|
||||
$large_style = ImageStyle::load('large');
|
||||
$medium_style = ImageStyle::load('medium');
|
||||
$node = $node_storage->load($nid);
|
||||
$image_uri = File::load($node->{$field_name}->target_id)->getFileUri();
|
||||
$this->assertRaw('<img srcset="' . $medium_style->buildUrl($image_uri) . ' 1x, ' . $large_style->buildUrl($image_uri) . ' 2x"');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests responsive image formatters linked to the file or node.
|
||||
*
|
||||
|
|
@ -451,6 +498,7 @@ class ResponsiveImageFieldDisplayTest extends ImageFieldTestBase {
|
|||
|
||||
// Output should contain all image styles and all breakpoints.
|
||||
$this->drupalGet('node/' . $nid);
|
||||
$this->removeWhiteSpace();
|
||||
switch ($link_type) {
|
||||
case 'file':
|
||||
// Make sure the link to the file is present.
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@
|
|||
* Available variables:
|
||||
* - sources: The attributes of the <source> tags for this <picture> tag.
|
||||
* - img_element: The controlling image, with the fallback image in srcset.
|
||||
* - output_image_tag: Whether or not to output an <img> tag instead of a
|
||||
* <picture> tag.
|
||||
*
|
||||
* @see template_preprocess()
|
||||
* @see template_preprocess_responsive_image()
|
||||
|
|
@ -13,7 +15,10 @@
|
|||
* @ingroup themeable
|
||||
*/
|
||||
#}
|
||||
<picture>
|
||||
{% if output_image_tag %}
|
||||
{{ img_element }}
|
||||
{% else %}
|
||||
<picture>
|
||||
{% if sources %}
|
||||
{#
|
||||
Internet Explorer 9 doesn't recognise source elements that are wrapped in
|
||||
|
|
@ -27,4 +32,5 @@
|
|||
{% endif %}
|
||||
{# The controlling image, with the fallback image in srcset. #}
|
||||
{{ img_element }}
|
||||
</picture>
|
||||
</picture>
|
||||
{% endif %}
|
||||
|
|
|
|||
Loading…
Reference in New Issue