You are here

function file_entity_field_attach_update in File Entity (fieldable files) 7.3

Implements hook_field_attach_update().

File

./file_entity.module, line 2776
Extends Drupal file entities to be fieldable and viewable.

Code

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'] = 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['title']) ? $item['title'] : '';
              }
            }
            field_attach_update('file', $file);
          }
        }
      }
    }
  }
}