Skip to content

Instantly share code, notes, and snippets.

@mrtag23
Created August 30, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrtag23/1974ea7a7f3c4dcd030e2fd5f32c3292 to your computer and use it in GitHub Desktop.
Save mrtag23/1974ea7a7f3c4dcd030e2fd5f32c3292 to your computer and use it in GitHub Desktop.
responsive media template override for drupal
{#
/**
* @file
* Theme override of a responsive image.
*
* 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()
*/
#}
{% set modifier_class = ('responsive-media--' ~ responsive_image_style_id|replace({'__' : '-'}))|clean_class %}
{% if output_image_tag %}
{{ img_element }}
{% else %}
<div class="responsive-media {{ modifier_class }}">
{# Lazyload image #}
<object data="{{ img_element['#uri'] }}" type="image/jpeg" class="responsive-media__placeholder js-img-placeholder">{{ img_element['#alt'] }}</object>
<picture class="responsive-media__picture">
{% if sources %}
{#
Internet Explorer 9 doesn't recognise source elements that are wrapped in
picture tags. See http://scottjehl.github.io/picturefill/#ie9
#}
<!--[if IE 9]><video style="display: none;"><![endif]-->
{% for source_attributes in sources %}
<source{{ source_attributes }}/>
{% endfor %}
<!--[if IE 9]></video><![endif]-->
{% endif %}
{# The controlling image, with the fallback image in srcset. #}
{{ img_element }}
</picture>
</div>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment