Problem/Motivation

When a site uses images from the media library, it takes a long chain of configuration to display images with a given responsive image style. When multiple node types (or other entities /w bundles) with multiple view modes require images with different styles, each variation requires its own chain of configuration.

Data chain: Node > Entity reference field > Image media > Image field
Format chain: Node view mode > Entity reference field formatter > Image media view mode > Image field formatter > Responsive image style

Proposed resolution

Introduce a new field formatter for entity reference fields that reference media entities to shorten the format chain.

New format chain: Node view mode > Responsive media image field formatter > Responsive image style

Remaining tasks

t.b.d.

User interface changes

New formatter for Entity reference fields referring to image media.

Release notes snippet

t.b.d.

Issue fork drupal-2947796

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

landsman created an issue. See original summary.

landsman’s picture

I got it, there is it:
Structure > Media bundles > Edit Image > Manage display

landsman’s picture

Status: Active » Closed (works as designed)
nortmas’s picture

It's a big downside.
If I have many different node types and displays with references to the media image, I need to create as many displays for media Image entity as many responsive image types I have.
Example:

node--article--slide reference to media--image--article-slide (shows image field like a specific responsive type 'article-slide')
node--article--teaser reference to media--image--article-teaser (shows image field like a specific responsive type 'article-teaser')
node--article--small reference to media--image--article-small (shows image field like a specific responsive type 'article-small')

Would be great to have the Responsive image field formatter for the entity-reference filed type.

nortmas’s picture

Version: 8.4.x-dev » 8.5.x-dev
Issue summary: View changes
Status: Closed (works as designed) » Active
nortmas’s picture

StatusFileSize
new2.61 KB

Here is the additional field formatter which implements the feature.

aisforaaron’s picture

I created a core patch from nortmas formatter file against core 8.5.x-dev to test out. I'm guessing this would go directly into the responsive_image module. I'm using composer to apply the patch. This is my first core module patch and had to make sure the path was /modules instead of /core/modules. Let me know if this isn't the proper way to setup a core patch.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

yoruvo’s picture

FYI, there is already a contrib module which adds this: Responsive Media Thumbnail

The file in that module has the difference of extending from *ResponsiveImageFormatter* rather than ImageFormatter like the example here does.

bobbygryzynger’s picture

Version: 8.6.x-dev » 8.8.x-dev
Status: Active » Needs review
StatusFileSize
new10.49 KB
new20.52 KB

Updated #7 to add the new field formatter to the FieldFormatter plugins that ship with the responsive_image module. Also cleaned-up a couple of coding standards violations.

FWIW, I think this should be a core patch rather than a contrib module since both responsive image and media entities are stable core features.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

samlerner’s picture

Updating the patch from #10 with one that removes /core from the patch path, since that is implied when applying the patch. In #10 the file is created in docroot/core/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php

jhedstrom’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

This is looking good. Just a few comments and code nits below. It should also have some tests to ensure it's behaving as expected.

  1. +++ b/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,303 @@
    +    $plugin_id,
    +    $plugin_definition,
    +    FieldDefinitionInterface $field_definition,
    +    array $settings,
    +    $label,
    +    $view_mode,
    +    array $third_party_settings,
    +    AccountInterface $current_user,
    +    ImageStyleStorageInterface $image_style_storage,
    +    RendererInterface $renderer,
    +    $responsive_image_style_storage,
    +    LinkGeneratorInterface $link_generator
    

    I wonder if rather than overriding the parent constructor, since it already takes so many parameters, if instead we could use the setter pattern in the create() method:

    Something like:

    $instance = parent::create(...);
    $instance->setRenderer(...);
    $instance->setResponsiveImageStorage(...);
    $instance->setLinkGenerator(...);
    

    where those 3 setter methods can be protected.

    (See this blog post for details on this pattern.)

    This is just a suggestion/point of discussion, not necessarily required (I'm not sure if core is using that pattern elsewhere yet or not.)

  2. +++ b/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,303 @@
    +      '#title' => t('Responsive image style'),
    ...
    +      '#title' => t('Link image to'),
    ...
    +      '#empty_option' => t('Nothing'),
    ...
    +      $summary[] = t('Responsive image style: @responsive_image_style', ['@responsive_image_style' => $responsive_image_style->label()]);
    ...
    +        'content' => t('Linked to content'),
    +        'file' => t('Linked to file'),
    ...
    +      $summary[] = t('Select a responsive image style.');
    

    Nit: should use $this->t() as elsewhere in the patch.

  3. +++ b/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,303 @@
    +    if ($style_id && $style = ResponsiveImageStyle::load($style_id)) {
    

    Since the class already has responsive image style storage injected, this can be $this->responsiveImageStyleStorage->load($style_id) I think.

  4. +++ b/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,303 @@
    +    if ($image_link_setting == 'content') {
    

    Nit, this should be ===

yusufhm’s picture

@jhedstrom thanks for the setter pattern suggestion; really happy I learnt about that; will come in handy in a lot of cases. Based on your comment on whether core is using it or not, I did a quick find and it is being used in a few places, one of them being Drupal\update\UpdateSettingsForm. They're not even using a setter method there, but instead setting the property directly, which I reproduced in my patch.

@SamLerner when I tried to apply your patch at #12, it was applied to the wrong directory; I had to add /core again to the patch file in order for it to apply properly, so I added it to my new patch as well. Other patches for core also seem to have that in.

I have addressed all of the points mentioned in #13 except for tests; will add some as soon as I can, if somebody else doesn't get to it first.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

andypost’s picture

batkor’s picture

#14 forked for me. Drupal 8.9.7

batkor’s picture

StatusFileSize
new8.4 KB
new1.23 KB

Update patch for 9.x
Replace entity.manager to entity_type.manager
See interdiff file

batkor’s picture

Status: Needs work » Needs review
andypost’s picture

Status: Needs review » Needs work

Formatter looks great, but issue still needs test coverage

BarisW’s picture

This is great work, really useful. It took me a while to get the patch in #19 working, until I found out that the new file was placed in /core/core/modules.. can't figure out why. But after I moved the file to the correct location, life was good!

batkor’s picture

BarisW add to your composer.file this line https://gitlab.com/batkor/ease/-/blob/master/composer.json#L41

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

dqd’s picture

Hm, maybe I do misunderstand the issue and its goal and the regarding patch but I cannot review or confirm the patch functionality since I do not see any change in the formatter options of a media reference field like I would expect here. Where should the responsive image format appear. I expect it to appear under /admin/structure/types/manage/nodetype/display when I choose a formatter for the media reference. Drupal 9.4.x dev with responsive image enabled and patch applied successfully via git apply -v.

sutharsan’s picture

Issue summary needs a description of the problem, etc. It currently has none.

sutharsan’s picture

  • +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,246 @@
    +  /**
    +   * @var \Drupal\Core\Entity\EntityStorageInterface
    +   */
    +  protected $responsiveImageStyleStorage;
    

    Add missing description.

  • +++ b/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php
    @@ -0,0 +1,246 @@
    +  public function settingsForm(array $form, FormStateInterface $form_state) {
    +    $responsive_image_options = [];
    +    $responsive_image_styles = $this->responsiveImageStyleStorage->loadMultiple();
    

    Add image style sorting as in \Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::settingsForm:

    uasort($responsive_image_styles, '\Drupal\responsive_image\Entity\ResponsiveImageStyle::sort');
    
  • sutharsan’s picture

    Issue summary: View changes
    Issue tags: -Needs issue summary update

    Issue summary added, please review.

    sutharsan’s picture

    The plugin should declare a dependency on the Responsive Image module. Without it the use of this plugin results in the below error:

    Drupal\Component\Plugin\Exception\PluginNotFoundException: The "responsive_image_style" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (regel 143 van /var/www/html/htdocs/core/lib/Drupal/Core/Entity/EntityTypeManager.php).

    batkor’s picture

    StatusFileSize
    new8.51 KB

    Fixed bugs from #28

    batkor’s picture

    StatusFileSize
    new1.59 KB

    Add interdiff #19 - #31

    catch’s picture

    Title: Responsive image format for entity reference to Media » Responsive image format for media thumbnails
    Related issues: +#3192234: Apply width and height attributes to allow responsive image tag use loading="lazy"

    This is really a responsive image formatter for media thumbnails so that it bypasses media display modes altogether. Retitling a bit and adding related issues - this will also need to add image loading attribute support if that lands first.

    Version: 9.4.x-dev » 9.5.x-dev

    Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    sutharsan’s picture

    The title change is not correct and is not in line with the functionality of this formatter. The formatter configuration allows selecting all possible responsive image styles, much more than the Thumbnail style. And more, the introducing a new formatter only suitable for the thumbnail image, make the added value of this formatter almost zero.

    Version: 9.5.x-dev » 10.1.x-dev

    Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    hitchshock’s picture

    Title: Responsive image format for media thumbnails » Responsive image format for media
    StatusFileSize
    new8.62 KB
    new8.48 KB
    new8.43 KB

    Reworked the patch to use the file from the media source field instead of the thumbnail.

    The ticket still needs tests for a new formatter.

    flyke’s picture

    Patch from #37 does not seem to apply to 9.5.3.

    git apply -v --directory=web responsive-style-for-media-9.5-37.patch
    Checking patch web/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php...
    error: while searching for:
          ],
        ];
    
        $link_types = [
          'content' => $this->t('Content'),
          'file' => $this->t('File'),
        ];
        $elements['image_link'] = [
          '#title' => $this->t('Link image to'),
          '#type' => 'select',
          '#default_value' => $this->getSetting('image_link'),
          '#empty_option' => $this->t('Nothing'),
          '#options' => $link_types,
        ];
    
        return $elements;
    
    error: patch failed: web/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php:148
    error: web/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveImageFormatter.php: patch does not apply
    Checking patch web/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php...

    Fixed it with a new patch, only a single character is different.

    Update: I think my problem is because the patch of this issue seems to collide with patch from https://www.drupal.org/project/drupal/issues/3192234
    (https://www.drupal.org/files/issues/2022-09-30/3192234-228-9.5.x.patch).

    flyke’s picture

    StatusFileSize
    new8.48 KB
    anybody’s picture

    Nice work, still needs tests as of #21, then it's ready for review.

    Version: 10.1.x-dev » 11.x-dev

    Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

    gaëlg’s picture

    carsteng’s picture

    Loading attribute is not set with that formatter!
    It is missing 2 lines before $elements[$delta] is set:
    When I find time I'll provide patch.

    $image_loading_settings = $this->getSetting('image_loading');
    $item_attributes['loading'] = $image_loading_settings['attribute'];
    
    $elements[$delta] = [
    .....
    

    immaculatexavier made their first commit to this issue’s fork.

    gabriel.passarelli’s picture

    StatusFileSize
    new8.62 KB

    Here's the #39 patch with the loading attribute that was missing

    deulenko’s picture

    StatusFileSize
    new8.62 KB

    The #45 patch causes this error
    ParseError: Unclosed '{' on line 164 in Composer\Autoload\{closure}() (line 173 of core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.php).
    so I've re-created it.

    Version: 11.x-dev » main

    Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

    Read more in the announcement.