1 file.module file_field_attach_load($entity_type, $entities, $age, $options)

Implements hook_field_attach_load().

File

core/modules/file/file.module, line 2700
Defines a "managed_file" Form API field and a "file" field for Field module.

Code

function file_field_attach_load($entity_type, $entities, $age, $options) {
  // Loop over all the entities looking for entities with attached images.
  foreach ($entities as $entity) {
    // field_purge_batch() calls hook_field_attach_load() on a stub entity,
    // which causes this to error as there is no ->bundle() property on these
    // stubs.
    if (method_exists($entity, 'bundle')) {
      $bundle = $entity->bundle();
    }
    else {
      return;
    }
    // Examine every image field instance attached to this entity's bundle.
    $instances = array_intersect_key(field_info_instances($entity_type, $bundle), _file_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) {
            // If alt and title text is not specified, fall back to alt and
            // title text on the file.
            if (!empty($item['fid']) && (empty($item['alt']) || empty($item['title']))) {
              $file = file_load($item['fid']);
              foreach (array('alt', 'title') as $key) {
                if (empty($item[$key]) && !empty($file->{$key})) {
                  $entity->{$field_name}[$langcode][$delta][$key] = $file->{$key};
                }
              }
            }
          }
        }
      }
    }
  }
}