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

Created on 8 November 2022, over 1 year ago
Updated 27 June 2024, 2 days 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 put an empty value for the alt text.
  4. Visit the media library and inspect the thumbnail image. Observe that the alt attribute is missing.

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

Needs work

Version

11.0 πŸ”₯

Component
MediaΒ  β†’

Last updated about 16 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.

  • First commit to issue fork.
  • Pipeline finished with Success
    3 days ago
    Total: 507s
    #208707
  • Status changed to Needs review 3 days ago
  • πŸ‡©πŸ‡ͺGermany sunlix Wesel

    Hey 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 in media_field_data which comes from the BaseFieldDefinition in the Media Entity itself.
    This property/column stay's null if you upload an image to a media entity based on the image source plugin and left the alt field empty.
    But the ImageItem field plugin already has a default value for alt which 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 updateThumbnail method in the Media entity.
    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_attribute defined.

  • Status changed to Needs work 2 days ago
  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Feels like something that should probably have some kind of test coverage even though it's a small change.

Production build 0.69.0 2024