Set filename as a default value to image 'Alt' & 'Title' field

Created on 14 March 2021, over 3 years ago
Updated 21 April 2024, 7 months ago

It would be nice to have a feature where user uploading an image and default Alt-Text value would get set to the filename. With the default values, users should be able to decide if they want to keep the same text or update the text as per their needs.

Feature request
Status

Needs work

Version

11.0 🔥

Component
Image module 

Last updated 16 days ago

Created by

🇮🇳India Nikhil Banait Pune

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.

  • Needs accessibility review

    Used to alert the accessibility topic maintainer(s) that an issue significantly affects (or has the potential to affect) the accessibility of Drupal, and their signoff is needed (see the governance policy draft for more information). Useful links: Drupal's accessibility standards, the Drupal Core accessibility gate.

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.

  • 🇺🇸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

  • 🇺🇸United States aitala

    Adding an attachment...

  • 🇮🇹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 !7352Draft: Resolve #3203489 "Set filename as" → (Closed) created by dwisnousky
  • Pipeline finished with Failed
    8 months ago
    Total: 174s
    #139232
  • 🇺🇸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.

Production build 0.71.5 2024