/**
* Implements hook_field_attach_update().
*/
function file_entity_field_attach_update($entity_type, $entity) {
// The other way around: you upload a new image on a node and enter alt/title tags.
// Save alt and title text from the image field to the file entity,
// but ONLY if the file entity's alt/title fields are empty.
if ($entity_type == 'node') {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
// Examine every image field instance attached to this entity's bundle.
$instances = array_intersect_key(field_info_instances($entity_type, $bundle), _file_entity_get_fields_by_type('image'));
foreach ($instances as $field_name => $instance) {
if (!empty($entity->{$field_name})) {
foreach ($entity->{$field_name} as $langcode => $items) {
foreach ($items as $delta => $item) {
$file = file_load($item['fid']);
if ($file) {
if (isset($file->field_file_image_alt_text) && empty($file->field_file_image_alt_text[$langcode][0]['value'])) {
$file->field_file_image_alt_text[$langcode][0]['value'] = $item['alt'];
}
if (isset($file->field_file_image_title_text) && empty($file->field_file_image_title_text[$langcode][0]['value'])) {
$file->field_file_image_title_text[$langcode][0]['value'] = $item['title'];
}
}
field_attach_update('file', $file);
}
}
}
}
}
}
I changed lines 2704 to 2709 as follows:
if (isset($file->field_file_image_alt_text) && empty($file->field_file_image_alt_text[$langcode][0]['value'])) {
$file->field_file_image_alt_text[$langcode][0]['value'] = isset($item['alt']) ? $item['alt'] : '';
}
if (isset($file->field_file_image_title_text) && empty($file->field_file_image_title_text[$langcode][0]['value'])) {
$file->field_file_image_title_text[$langcode][0]['value'] = isset($item['alt']) ? $item['alt'] : '';
}
Am I crazy?
Comments
Comment #2
joseph.olstadya that change is only half correct
the second one should be:
field_file_image_title_text expects a title, not an alt.
Comment #3
tondeuse commentedI am experiencing this issue with 7.x-3.0-beta16 (1592259288) as well, after migrating a site from the Media 7.x-1.x branch. I have weird issues, this was on of them, first Drupal site 9 years ago, heu-hem... Thanks to mcdoolz for padding this declaration with a safety for edge cases, it fixes the following error in node edit forms where images are attached to fields with Drupal 7.72 && PHP 7.4. Patch attached.
Comment #4
tondeuse commentedSorry for the glitch, this new patch uses similar spaces around the ternary separator.
Comment #5
joseph.olstadComment #7
joseph.olstadfixed in dev branch