Problem/Motivation
Media derive their thumbnail images and alt text for those images from their media source plugins. For image-based media, that's the "Image" media source plugin. When the plugin is asked to present the alt text for its thumbnail image, it returns the alt text stored in the image field of the media entity. This works great, unless the alt text is an empty string. The Image plugin treats this as a "falsey" value and ends up returns NULL for the alt text instead of the empty string.
An empty string value for the alt text attribute (alt="") has important meaning for accessibility. It instructs assistive tech that the image is decorative and should be ignored by screen readers. Drupal's image formatter template will correctly output the alt="" attribute on the image element in this case. If the alt is NULL, then Drupal's image formatter template won't print the alt attribute at all. This is very problematic as assistive tech may instead announce the image filename when it comes upon the image, because it's assuming the missing alt attribute is a mistake and the image may be important for the user to know about.
Steps to reproduce
- Install media module.
- Edit the image field on the Image media type and uncheck the Alt text required checkbox and save.
- Add an Image media entity - upload an image and leave empty alt text field.
- Visit the media library page: /admin/content/media and inspect the thumbnail image. Observe that the alt attribute is missing.
- Then inspect image tag. you 'll notice if nothing pass in alt text field then alt tag 'll not display.
Proposed resolution
This is a simple fix. Modify the getMetadata method of the Image media source plugin so that it if the alt text of the associated media entity is an empty string, return the same empty string.
Remaining tasks
User interface changes
API changes
Data model changes
No. Could have an update function to update all existing media thumbnails to swap NULL thumbnail alt values with an empty string instead, but that's probably not worth it. Some sites will have tens of thousands of media items and I guess this update function could take a while to run.
Release notes snippet
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | patch_applied.jpg | 89.45 KB | gaurav-mathur |
| #5 | afterpatch.jpg | 60.54 KB | gaurav-mathur |
| #2 | 3319601-thumbnail-empty-alt-2.patch | 729 bytes | bkosborne |
Issue fork drupal-3319601
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:
- 3319601-media-image-thumbnail
changes, plain diff MR !8548
Comments
Comment #2
bkosborneGitlab integration is currently down, so cannot create an issue fork. In the meantime, here's a normal patch. I guess this needs tests but lets see if this causes any to fail.
Comment #3
ressaSee these Gitlab issue, hopefully it will be fixed soon:
Comment #4
bkosborneComment #5
gaurav-mathur commentedApplied patch #2 on the drupal version 10.1.x successfully. It looks good to me.
Refer to the screenshot
Thankyou
Comment #6
rutiolmaIn fact the problem doesn't seem to be only with the thumbnail on content administration but any image rendered as a thumbnail doesn't have an alt attribute.
To test, follow the described steps on this issue and then just add a media image field to a content type and set its view display as "thumbnail". You'll notice that there's no alt attribute for the image when you view the node.
This problem doesn't exist if you set the view display as "render entity".
Not sure if this is by design but there are several related issues on contrib modules.
Comment #7
bkosborneDoes the patch not resolve that though? Did you test it? Wondering why you set the status back to Needs Work
Comment #8
rutiolmaThe patch does work for the administration view (which this issue is about) but IMHO this should be tackled in a more generic way because the problem seems to be related with thumbnails in general.
Comment #9
meeni_dhobale commentedI tried to reproduce this issue, and observed that, this thumbnail issue only occur on
/adminpages, I tried to reproduce in other pages as well. I create one view and add some content images as thumbnail, but in the view there is already alt attribute is present. For admin pages this patch which mentioned in #2 are work properly.@rutiolma can you please provide some steps to reproduce same issue which you talk about.
Comment #10
mgiffordAdding tag for WCAG SC 1.1.1
Comment #14
sunlixHey together,
I have research a little bit on this and I am sure, that the patch in die issue fork solve this.
The related property here is
thumbnail__alt(Database column inmedia_field_datawhich comes from the BaseFieldDefinition in theMediaEntity itself.This property/column stay's
nullif you upload an image to a media entity based on the image source plugin and left thealtfield empty.But the ImageItem field plugin already has a default value for
altwhich is an empty string.see: https://git.drupalcode.org/issue/drupal-3319601/-/blob/3319601-media-ima...
For remote video media entities based on the oembed source plugin this issue doesn't come up because the default value is set on the
updateThumbnailmethod in theMediaentity.see: https://git.drupalcode.org/issue/drupal-3319601/-/blob/3319601-media-ima...
So the issue is "only" related to media entities which are based on media source plugins that have
$thumbnail_alt_metadata_attributedefined.Comment #15
smustgrave commentedFeels like something that should probably have some kind of test coverage even though it's a small change.
Comment #17
pooja_sharma commentedAfter applying patch , if you want test alt attribute display or not if nothing pass in alt text field then test with new media
Don't try to update alt text of existing media it is part of other issue: https://www.drupal.org/project/drupal/issues/3232414
I got bit confused whether patch is actually working or not.
Comment #18
pooja_sharma commentedAdded test coverage in a distinct test file to assess specific scenario.
Please review, move NR
Comment #19
pooja_sharma commentedComment #20
sunlix@pooja_sharma
Thank you a lot for pushing this forwars with a functional test. :-)
I have changed the test cases a bit to have to specific test case for given strings and a test case with no given alt attribute.
Both cases have to result in having a rendered alt attribute.
I am unsure if we have to add a specific alt = NULL testcase, because this is not intended by the used field API here.
ImageItemhave default values for it's properties which should not altered or?Comment #21
lendudeNice, the test coverage isn't triggering the bug it seems though, the test-only run https://git.drupalcode.org/issue/drupal-3319601/-/jobs/2003864 is green
Comment #22
pooja_sharma commentedThanks for review, recent code changes lead to pass test case in every scenario which is not correct , so revert back changes.
Alt attribute field is bind with image media entity if alt attribute not attach with empty string then it 'll not be use of adding this specific test case as in manual test if we keep empty "attribute field" then alt tag not render on FE which leads to issue.
PLease review, moving to NR
Comment #23
pooja_sharma commentedComment #24
sunlixmea culpa, I did make an understanding failure. Thought it is defaulting to the
ImageItemdefault settings, which is not, because here is no field api involved in the test szenario. :-)Thank you for pointing that out, did learn something new with testing! :)
Should be good to go.
Comment #25
pooja_sharma commentedRebased the MR with latest code, seems working fine
Comment #26
smustgrave commentedLeft a comment on the MR.
Comment #27
pooja_sharma commentedAddressed the mentioned suggestions & removed the newly created test file as test code moved to existing suggested test file.
Please review , moving NR
Comment #28
pooja_sharma commentedComment #29
smustgrave commentedLeft some comments on the MR
Comment #32
pooja_sharma commentedApplied suggestion & left comments as well.
Please review, moving NR
Comment #33
smustgrave commentedWill leave in review then for others but think the other test location makes more sense since the test there is checking the media content view while MediaSourceImageTest is not.
Comment #34
pooja_sharma commentedApplied the suggestions, Moved test coverage code from MediaSourceImageTest to MediaOverviewPageTest
Comment #35
smustgrave commentedRe-ran failure for the editor module and appeared to be random.
Believe all feedback for this one has been addressed.
Comment #36
quietone commentedThanks for continuing to work on this!
I left some comments in the MR. I am also not sure that this is the best way to test this. I'll ask about that.
Comment #37
pooja_sharma commentedAddressed the feedback, Please review the MR, moving NR.
Comment #38
sunlixAdded a review for proposing null coalescing operator. So the change will be just 1 character. :-P
Thank you @pooja_sharma for your fast interaction on this!
Comment #39
smustgrave commentedFeedback from #38
Yes thanks @pooja_sharma for keeping this one alive.
Comment #40
pooja_sharma commentedAddressed the requested changes, Please review, moving NR.
Comment #41
smustgrave commentedBelieve feedback has been addressed.
Comment #42
longwaveSome nits about the test case.
Comment #43
pooja_sharma commentedComment #44
pooja_sharma commentedAddressed the feedback, rebased the MR, apart from it nothing seems to be left.
Please review, moving NR.
Comment #45
smustgrave commentedBelieve feedback around test has been addressed.
Comment #46
quietone commentedI didn't find any unanswered questions here. Updated the credit, though it should be check by the committer.
I made suggestions to comments in the test and applied them. They are both minor and I double checked them, so I am leaving this at RTBC
Comment #51
catchCommitted/pushed to 11.x and cherry-picked back through to 10.3.x, thanks!