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.

Issue fork drupal-3203489

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Nikhil Banait created an issue. See original summary.

nikhil banait’s picture

Status: Active » Needs review
StatusFileSize
new147.81 KB
new4.36 KB

Added a configuration to the image field setting.
set default alt value

Status: Needs review » Needs work
vakulrai’s picture

Status: Needs work » Needs review
StatusFileSize
new48.79 KB
new189.56 KB
new2.04 KB
new4.88 KB

Patch from #2 is working and applied cleanly. But is a scenario which I think is valid :

BY default on creating a new image field the "Alt text held is required" which if we let as its and "'Set uploaded filename as default Alternative text" is checked, the user can still overwrite the behaviour of the patch from the node page check: Filed-with-alt-required.png.

Now the scenario becomes "set default filename as text " with overwriting it from node form.

Added Code to prevent this behaviour: Now if while creating the image field "'Set uploaded filename as default Alternative text" input is Checked then required alt text filed will become unrequired: Field-state-change.png

vakulrai’s picture

StatusFileSize
new4.88 KB

Fixed Some Code Standards Issue .

Status: Needs review » Needs work

The last submitted patch, 5: set_filename_as_default_value-3203489-4.patch, failed testing. View results

nikhil banait’s picture

@vakulrai Thanks for updating the patch. I would recommend to remove the changes regarding making 'Alt field required' unchecked when 'Set uploaded filename as default Alternative text' is checked. Actually setting up default value & making any field required are two different concepts. However default value is supposed to be changed by editor always. For good SEO, these fields(alt & title) has to be mandatory. It's upto an editor to override default value or not.

I hope you understand my view.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

tommiecrawford’s picture

Patch #2 and #5 didn't worked for me, recreated it.

tommiecrawford’s picture

Fix typo and set default values.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

mhmhartman’s picture

Recreated the patch for Drupal 9.5.1

_utsavsharma’s picture

StatusFileSize
new887 bytes
new1.78 KB

Tried to fix CCF for #14.

_utsavsharma’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 15: 3203489-15.patch, failed testing. View results

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

aitala’s picture

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

aitala’s picture

StatusFileSize
new573.22 KB

Adding an attachment...

dgsiegel’s picture

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!

charles belov’s picture

Adding accessibility and needs accessibility review tags. In my experience, file names are typically not appropriate as alt text.

dwisnousky made their first commit to this issue’s fork.

charles belov’s picture

Has an accessibility review been done on this issue?

falcon03’s picture

The strongest possible -1 from an accessibility standpoint. This feature would encourage a bad practice that unfortunately is very popular on the Internet.

charles belov’s picture

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.

calydia’s picture

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.

bohus ulrych’s picture

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.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.