/**
 * 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

mcdoolz created an issue. See original summary.

joseph.olstad’s picture

ya that change is only half correct

the second one should be:


 $file->field_file_image_title_text[$langcode][0]['value'] = isset($item['title']) ? $item['title'] : '';

field_file_image_title_text expects a title, not an alt.

tondeuse’s picture

I 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.

Notice : Undefined index: title in file_entity_field_attach_update() (ligne 2795 in /var/www/html/sites/all/modules/contrib/file_entity/file_entity.module).
tondeuse’s picture

Sorry for the glitch, this new patch uses similar spaces around the ternary separator.

joseph.olstad’s picture

Status: Active » Reviewed & tested by the community

  • joseph.olstad committed 6892784 on 7.x-3.x authored by tondeuse
    Issue #2993186 by tondeuse, joseph.olstad, mcdoolz: Undefined index: alt...
joseph.olstad’s picture

Status: Reviewed & tested by the community » Fixed

fixed in dev branch

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.