Media image thumbnail incorrectly ends up as NULL when it should be an empty string

Created on 8 November 2022, over 2 years ago
Updated 18 September 2024, 6 months ago

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

  1. Install media module.
  2. Edit the image field on the Image media type and uncheck the Alt text required checkbox and save.
  3. Add an Image media entity - upload an image and leave empty alt text field.
  4. Visit the media library page: /admin/content/media and inspect the thumbnail image. Observe that the alt attribute is missing.
  5. 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

🐛 Bug report
Status

RTBC

Version

11.0 🔥

Component
Media 

Last updated about 12 hours ago

Created by

🇺🇸United States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
  • Accessibility

    It affects the ability of people with disabilities or special needs (such as blindness or color-blindness) to use Drupal.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

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

Production build 0.71.5 2024