I have came with this alternative solution if you have have existing thumbnails images and you want to add to alt text to existing images
then i have done that by doing this
function custom_module_entity_load(array &$entities, $entity_type) {
if ($entity_type === 'media') {
foreach ($entities as $media) {
if ($media->hasField('field_media_image') && !$media->get('field_media_image')->isEmpty()) {
$media_image_field = $media->get('field_media_image');
$alt_text = $media_image_field->first()->get('alt')->getValue();
$media_target_id = $media_image_field->first()->get('target_id')->getValue();
if ($media->hasField('thumbnail') && !$media->get('thumbnail')->isEmpty()) {
$thumbnail_field = $media->get('thumbnail');
$thumbnail_item = $thumbnail_field->first();
if ($media_target_id === $thumbnail_item->get('target_id')->getValue()) {
$thumbnail_item->set('alt', $alt_text);
}
}
}
}
}
}
So far what i have done is in my custom module using form alter hook
**
* Implements hook_form_alter().
*/
function custom_module_form_alter(&$form, FormStateInterface $form_state) {
if ($form_state->getFormObject() instanceof \Drupal\media\MediaForm) {
$form['actions']['submit']['#submit'][] = 'custom_module_process_alt_text';
}
}
/**
* Custom submit handler to set alt text for media images and thumbnails.
*/
function custom_module_process_alt_text(array &$form, FormStateInterface $form_state) {
$media = $form_state->getFormObject()->getEntity();
if ($media->hasField('field_media_image')) {
$current_alt_text = $media->get('field_media_image')->first()->get('alt')->getValue();
if (!empty($current_alt_text)) {
$reflection = new \ReflectionClass($media);
try {
$method = $reflection->getMethod('updateThumbnail');
$method->setAccessible(true);
$thumbnail_field = $media->get('thumbnail');
if ($thumbnail_field) {
$thumbnail_field->alt = $current_alt_text;
}
$method->invoke($media);
}
catch (\ReflectionException $e) {
\Drupal::logger('simple_media_bulk_upload')->error('Error updating thumbnail alt text: @message', [
'@message' => $e->getMessage(),
]);
}
$media->save();
}
}
}
this code is useful when in manage display you are using format as thumbnail i have done this way
The alt text is skipped in media image when uploading through bulk upload and in manage display format is selected as thumbnail but it works fine we change the format to render entity than it works fine
hamza_niazi → created an issue.
I faced that issue too while on drupal 10.3.6 i tried to uploda images using bulk upload but alt text was missing in img attribute
I m facing the same issue with view using Exposed form style: basic
Update the patch to work with the latest release of textfield_counter thanks to the work done by #2 and #3
I updated the code to reference the validation method using the class name (static::class) instead of dynamically deriving it. This ensures the method is correctly called on the class using the trait, resolving the instantiation error.
The issue in #8 patch was this code originally attempted to call the validateFieldFormElement method using a dynamically derived class name from traits, which caused an error because traits cannot be instantiated or referenced in this manner. So i update the patch
@pgn5qs i m facing the same issue can you kindly tell me the steps that how you have over come that issue Kindly
Hi , @paulard i m using contributed module html formatter → In manage display when i select format as Html formmater and then link to the content does not work
hamza_niazi → created an issue.
This error comes after the migration i have done migration of my drupal from 9.5.10 to 10.0.11 my full calendar is not loading and is throwing the error of the calendar_option in console
#2 patch does not work when doing migration from 9 to 10 because it does not update the composer.lock file
hamza_niazi → created an issue.
Patch for D10