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

Created on 14 March 2021, over 4 years ago
Updated 7 July 2023, about 2 years 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 9 days ago

Created by

🇮🇳India Nikhil Banait Pune

Live updates comments and jobs are added and updated live.
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
    over 1 year 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.

  • 🇨🇿Czech Republic Bohus Ulrych Pilsen (Czechia)

    My two cents - for me this is nice feature. My files (filenames) are already well organized with proper names and generate alt from the filename save my time.
    FYI #21 patch works - but only with one file. For all other alts it always use alt from the first image.

Production build 0.71.5 2024