- 🇺🇸United States aitala
There is an issue with this patch when you are uploading multiple images into a field.
All the images get Alt text from the first image name.
This is with Drupal 9.5.10 and the Plupload Integration Module with unlimited number of images. Note that I don't think plupload is being used for the image field here.
Thanks,
Eric - 🇮🇹Italy dgsiegel
As this patch didn't work for us in Drupal 10.x, we've decided to handle this in a custom module. We're appending a custom callback in the widget hook for
image_image
and setting the default value of the alt tag there:<?php /** * Implements hook_field_widget_single_element_WIDGET_TYPE_form_alter() for image_image */ function mymodule_field_widget_single_element_image_image_form_alter(array &$element, \Drupal\Core\Form\FormStateInterface $form_state, array $context) { $element['#process'][] = "mymodule_image_widget_process"; } /** * Prefill alt tags in image widget on upload */ function mymodule_image_widget_process($element, \Drupal\Core\Form\FormStateInterface $form_state, $form) { if (!empty($element['#files']) && $element['#preview_image_style']) { if (empty($element['alt']['#default_value'])) { $file = reset($element['#files']); $filename = pathinfo($file->getFileName(), PATHINFO_FILENAME); $clean = str_replace(array('-', '_'), ' ', $filename); $element['alt']['#default_value'] = $clean; } } return $element; }
Hope this helps!
- 🇺🇸United States charles belov San Francisco, CA, US
Adding accessibility and needs accessibility review tags. In my experience, file names are typically not appropriate as alt text.
- First commit to issue fork.
- Merge request !7353Draft: Resolve #3203489 "Img default field values" → (Open) created by dwisnousky
- 🇺🇸United States charles belov San Francisco, CA, US
Has an accessibility review been done on this issue?
- 🇮🇹Italy falcon03
The strongest possible -1 from an accessibility standpoint. This feature would encourage a bad practice that unfortunately is very popular on the Internet.
- 🇺🇸United States charles belov San Francisco, CA, US
Noting that the filename coming from an iPhone is typically just IMG followed by a sequence number, something that is not accessible. I also see many image file names on our website that are gibberish.
- 🇫🇮Finland Calydia
As people above have already commented, this would be bad for accessibility. The purpose of the alt text is to describe the image, which a filename in most cases just doesn't do.