Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Varbase FAQs 9.2.1 Minor update available for module varbase_faqs (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

Media Twig Tools

42 sites Security covered
View on drupal.org

This module simplifies the integration of media entities, particularly images, into Twig templates. It cleans up default rendered markup and provides tools to manage responsive image settings, including srcset and picture elements, directly within Twig without needing extensive configuration.

CONTENTS OF THIS FILE
---------------------

* Introduction
* Requirements
* Installation
* Configuration
* Maintainers

INTRODUCTION
------------

The Media Twig Tools modules brings some features to facilitate the integration
of media entity in twig template, especially for images and pictures. By
default, media are rendered with the drupal rendering process, bringing
potential unnecessary tags to display simple image and picture tags. Media Twig
Tools clean the default rendered markup of images.

It also brings some tools to decouple the theme sources and the configuration
for images. By the default, Drupal brings the image style configuration to deal
with the adaptive / responsive image. This can be very difficult to a front end
developper to integrate these practices when he has to deal with a lot of
several configurations for several image sizes and rendering pecularities. Media
Twig Tools provides tools to integrate srcSet settings, and picture settings
(with media queries) directly in twig without needing the definition of specific
configuration for each breakpoint, each image style etc... Media Twig Tools
allow front-end developpers to generate styled images at runtime.

REQUIREMENTS
------------

This module requires drupal/media modules.

INSTALLATION
------------

Install the Media Twig Tools module as you would normally install a contributed
Drupal module. Visit https://www.drupal.org/node/1897420 for further
information.

USE
-------------

### Render an image tag with srcset

Here is an example of how to render an image with src set and a customise the
DOM in twig.

{{ imgFromMedia(
        content.field_media_1,              // The media to display.

        // Les options
        {
            style: 'landscape',             // The image style base.
            class:'bordered grow',          // Custom image class.
            attributes:{ title:'Title', alt:'Overriden alt', 'data-other':'other data'}, // Custom attributes

            // Source set :
            srcset: {
              '1x': {                       // media query
                style: 'portrait',          // Base image style
                rules: [                    // List of new image style rules to apply
                  {
                    id: 'image_crop',       // Rule type
                    data: {
                      'width': 100,         // Rule data
                      'height': 200,
                    }
                  }
                ]
              },
              '2x': 'default'               // 'default' to display the default image.
          }
    } ) }}

Result :

<img title="Title" alt="Overriden alt" data-ok="other data"
     class="bordered grow"
     srcset="/sites/default/files/builder/portrait/width100height200image_crop/1_image.png 1x, /sites/default/files/styles/landscape/public/images/image.png?h=dae1a89e 2x"
     src="/sites/default/files/styles/landscape/public/images/image.png?h=dae1a89e"
     loading="lazy"/>

### Render a picture tag

Here is an example of how to render a picture tag with src set and a customise
the DOM in twig.

{{ pictureFromMedia(
    content.field_media_1,
    {
      style:'landscape',
      class:'class1 class2',
      attributes: {
        'data-other': 'other data',
        'title': 'Title'
      },
      settings: {
        '(min-width: 1024px)':{
          style: 'landscape',
          srcset: {
            '2x': 'default',
            '1x': {
              rules: [
                {
                  id: 'image_scale',
                  data: {
                    width: 2000,
                    height: 1000
                  }
                }
              ]
            }
          }
        },
        '(max-width: 1024px)': {
          style: 'portrait',
          srcset: {
            '2x': 'default',
            '1x': {
              rules: [
                {
                  id: 'image_scale_and_crop',
                  data: {
                    width: 200,
                    height: 300
                  }
                }
              ]
            }
          }
        }
      }
    })
  }}

Result :

<picture>
  <source
    srcset="/sites/default/files/styles/landscape/public/images/mac_logo-noir-bleu.png?h=dae1a89e 2x,/sites/default/files/builder/landscape/width2000height1000image_scale/4_mac_logo-noir-bleu.png 1x"
    media="(min-width: 1024px)">
  <source
    srcset="/sites/default/files/styles/portrait/public/images/mac_logo-noir-bleu.png?h=251a1d33 2x,/sites/default/files/builder/portrait/width200height300image_scale_and_crop/4_mac_logo-noir-bleu.png 1x"
    media="(max-width: 1024px)">
  <img
    data-other="other data" title="Title" class="class1 class2"
    alt="Move and co"
    src="/sites/default/files/styles/landscape/public/images/mac_logo-noir-bleu.png?h=dae1a89e"
    loading="lazy">
</picture>

CONFIGURATION
-------------

1. Navigate to Administration > Configuration > Media > Media Twig Tools.

Configurable parameters:

* Settings / Built image directory name : The directory where custom images are
built.
* Settings / No image style identifier : The default identifier to indicate
Media Twig Tools to not use any image style. Make sure it does not match an
existing image style name.

MAINTAINERS
-----------

* Thomas Sécher : https://www.drupal.org/u/tsecher

Activity

Total releases
4
First release
Feb 2025
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 4
Maintenance
Dormant

Release Timeline

Releases

Version Type Release date
3.0.2 Stable Jun 30, 2025
3.0.1 Stable May 12, 2025
3.0.0 Stable Apr 1, 2025
3.0.0-alpha2 Pre-release Feb 24, 2025