Add support for media reference fields

Created on 16 March 2018, over 6 years ago
Updated 22 June 2023, about 1 year ago

Problem/Motivation

The formatter provided by this module can be only used with image fields and despite there are some workarounds to make media reference field to work as mentioned in comment #6 ✨ Add support for media reference fields Needs review is needed to provide an easier way to use it with media fields.

Steps to reproduce

  • Add a media entity reference field to a content type.
  • Go to manage display setting for the content type used in previous step.
  • Try to select Background Image or Responsive Background image formatter for the media field.

Proposed resolution

Add support to use the formatter for media reference fields in the same way as image fields.

Original Issue from @W01F

I got this to work with paragraphs - but could not figure out how to target the image field of a media type correctly. The media reference field in the content type is set to display as "Rendered entity" and then in the media image content type settings I've set it to what should be the correct css selector of ".paragraph--id--[paragraph:id] .field--name-field-'field-name' .field--item", but neither that nor any of the other variations I tried seem to work.

✨ Feature request
Status

Needs review

Version

1.0

Component

Code

Created by

🇺🇸United States W01F

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States alex.verhoeven

    I was not able to get #6 to work entirely for paragraphs (works great for page headers where there is a single target selector), and really wanted the responsive background image to be applied to the outer paragraph div itself, rather than the media item.

    Here's what works for me...

    I followed the steps in #6 but I did not alter the media.html.twig file. Instead, I use the Twig template for the paragraph, where I am already setting some other, unrelated classes. In that Twig template, I added:

    {# get paragraph background image media entity ID and set as class #}
    {% if content.field_component_background_image|render %}
      {% set item = content.field_component_background_image['#items'][0] %}
      {% set bgimage_classes = ["bg-container-" ~ item.entity.id] %}
    {% endif %}
    
    {# Sets Paragraph type, view mode, and ID as classes #}
    {%
    set classes = [
    'paragraph',
    'paragraph--type--' ~ paragraph.bundle|clean_class,
    view_mode ? 'paragraph--view-mode--' ~ view_mode|clean_class,
    'paragraph--id--' ~ paragraph.id.value,
    ]
    %}
    
    {# Merges paragraph background image media entity ID with classes. #}
    {% set bgimage_field = content.field_component_background_image|render %}
    {% if bgimage_field %}
      {% set classes = classes|merge(bgimage_classes) %}
    {% endif %}
    
    {# Prints div with classes and contents of field_copy #}
    <div{{ attributes.addClass(classes) }}>
    
    {# YOUR CONTENT HERE #}
    {% if content.field_copy|render %}
      {{ content.field_copy }}
    {% endif %}
    
    </div>
    
  • 🇮🇪Ireland frankdesign

    @alex.verhoeven - you almost have it. All you need to do is add an open and close parentheses after id. So you code should be

    {% set bgimage_classes = ["bg-container-" ~ item.entity.id()] %}

Production build 0.69.0 2024