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.
| Comment | File | Size | Author |
|---|---|---|---|
| #46 | responsive-style-for-media-9.5-46.patch | 8.62 KB | deulenko |
| #45 | responsive-style-for-media-9.5-45.patch | 8.62 KB | gabriel.passarelli |
| #39 | responsive-style-for-media-9.5-38.patch | 8.48 KB | flyke |
| #37 | responsive-style-for-media-9.4-37.patch | 8.43 KB | hitchshock |
| #37 | responsive-style-for-media-9.5-37.patch | 8.48 KB | hitchshock |
Issue fork drupal-2947796
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
Comment #2
landsman commentedI got it, there is it:
Structure > Media bundles > Edit Image > Manage display
Comment #3
landsman commentedComment #4
nortmas commentedIt'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:
Would be great to have the Responsive image field formatter for the entity-reference filed type.
Comment #5
nortmas commentedComment #6
nortmas commentedHere is the additional field formatter which implements the feature.
Comment #7
aisforaaron commentedI 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.
Comment #9
yoruvo commentedFYI, 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.
Comment #10
bobbygryzyngerUpdated #7 to add the new field formatter to the
FieldFormatterplugins that ship with theresponsive_imagemodule. 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.
Comment #12
samlerner commentedUpdating the patch from #10 with one that removes
/corefrom the patch path, since that is implied when applying the patch. In #10 the file is created indocroot/core/core/modules/responsive_image/src/Plugin/Field/FieldFormatter/ResponsiveMediaImageFormatter.phpComment #13
jhedstromThis is looking good. Just a few comments and code nits below. It should also have some tests to ensure it's behaving as expected.
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:
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.)
Nit: should use
$this->t()as elsewhere in the patch.Since the class already has responsive image style storage injected, this can be
$this->responsiveImageStyleStorage->load($style_id)I think.Nit, this should be
===Comment #14
yusufhm@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
/coreagain 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.
Comment #17
andypostComment #18
batkor#14 forked for me. Drupal 8.9.7
Comment #19
batkorUpdate patch for 9.x
Replace
entity.managertoentity_type.managerSee interdiff file
Comment #20
batkorComment #21
andypostFormatter looks great, but issue still needs test coverage
Comment #22
BarisW commentedThis 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!
Comment #23
batkorBarisW add to your
composer.filethis line https://gitlab.com/batkor/ease/-/blob/master/composer.json#L41Comment #26
dqdHm, 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.Comment #27
sutharsan commentedIssue summary needs a description of the problem, etc. It currently has none.
Comment #28
sutharsan commentedAdd missing description.
Add image style sorting as in
\Drupal\responsive_image\Plugin\Field\FieldFormatter\ResponsiveImageFormatter::settingsForm:Comment #29
sutharsan commentedIssue summary added, please review.
Comment #30
sutharsan commentedThe 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).
Comment #31
batkorFixed bugs from #28
Comment #32
batkorAdd interdiff #19 - #31
Comment #33
catchThis 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.
Comment #35
sutharsan commentedThe 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.
Comment #37
hitchshockReworked the patch to use the file from the media source field instead of the thumbnail.
The ticket still needs tests for a new formatter.
Comment #38
flyke commentedPatch from #37 does not seem to apply to 9.5.3.
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).
Comment #39
flyke commentedComment #40
anybodyNice work, still needs tests as of #21, then it's ready for review.
Comment #42
gaëlgComment #43
carsteng commentedLoading 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.
Comment #45
gabriel.passarelli commentedHere's the #39 patch with the loading attribute that was missing
Comment #46
deulenko commentedThe #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.