Using the Responsive Image module

The Responsive Image module provides a field formatter for image fields that use the HTML5 picture tag and source sets. Utilizing the Breakpoint module, mappings to breakpoints are made to denote an image style to be used at each breakpoint.

The responsive image field formatter works with using a defined responsive image style. Responsive image styles are configurations that map image formats to specific breakpoints and modifiers. First, you need to define a responsive image style, and then you can apply it to an image field.

In this recipe, we will create a responsive image style set called Article image and apply it to the Article content type's image field.

Getting ready

You will need to enable the Responsive Image module as it is not automatically enabled with the standard installation.

How to do it...

  1. Go to Configuration and then to Responsive image styles under the Media section. Click on Add responsive image style to begin creating a new style set.
  2. Provide a label that will be used to administratively identify the Responsive image style set.
  3. Select a breakpoint group that will be used as a source of breakpoints to define the image style map.
  4. Each breakpoint will have a fieldset. Expand the fieldset and select a single image style, and then, pick an appropriate image style:
    How to do it...
  5. Additionally, choose a fallback image style in the event of a browser that doesn't support source sets, such as Internet Explorer 8.
  6. Click on Save to save the configuration, and add the new style set:
    How to do it...
  7. Go to Structure and Content types, and select Manage Display from the Article content type's drop-down menu.
  8. Change the Image field's formatter to Responsive image.
  9. Click on the Settings tab of the field formatter to choose your new Responsive image style set. Select Article image from the Responsive image style dropdown:
    How to do it...
  10. Click on Update to save the field formatter settings, and then click on Save to save the field display settings.

How it works...

The Responsive image style provides three components: a responsive image element, the responsive image style configuration entity, and the responsive image field formatter. The configuration entity is consumed by the field formatter and displayed through the responsive image element.

The responsive image style entity contains an array of breakpoints to image style mappings. The available breakpoints are defined by the selected breakpoint groups. Breakpoint groups can be changed anytime; however, the previous mappings will be lost.

The responsive image element prints a picture element with each breakpoint defining a new source element. The breakpoint's media query value is provided as the media attribute for the element.

Note

For Internet Explorer 9, Drupal 8 ships with the picturefill polyfill. Internet Explorer 9 does not recognize source elements wrapped by a picture element. The polyfill wraps the sources around a video element within the picture element.

There's more...

Performance first delivery

A benefit of using the responsive image formatter is performance. Browsers will only download the resources defined in the srcset of the appropriate source tag. This not only allows you to a deliver a more appropriate image size but also a smaller payload on smaller devices.

Removing picturefill polyfill

The Responsive Image module attaches the picturefill library to the responsive image element definition. The element's template also provides HTML to implement the polyfill. The polyfill can be removed by overriding the element's template and overriding the picturefill library to be disabled.

The following snippet, when added to a theme's info.yml, will disable the picturefill library:

libraries-override:
  core/picturefill: false

Then, the responsive-image.html.twig must be overridden by the theme to remove the extra HTML generated in the template for the polyfill:

  1. Copy responsive-image.html.twig from core/modules/responsive_image/templates to the theme templates folder.
  2. Edit responsive-image.html.twig and delete the Twig comment and IE conditional to output the initial video tag.
  3. Remove the last conditional, which provides the closing video tag.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset