You are here

function i18n_sync_field_file_sync in Internationalization 7

Sync a file or image field (i18n_sync_callback)

  • file-id's (fid) are synced
  • order of fid's is synced
  • description, alt, title is kept if already existing, copied otherwise
1 string reference to 'i18n_sync_field_file_sync'
i18n_sync_field_info_alter in i18n_sync/i18n_sync.module
Implements hook_field_info_alter()

File

i18n_sync/i18n_sync.module, line 304
Internationalization (i18n) package. Synchronization of translations

Code

function i18n_sync_field_file_sync($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_language) {
  $field_name = $instance['field_name'];

  // Build a copy of the existing files in the translation node
  // indexed by fid for easy retrieval in the copy loop below
  $existing_files = array();
  $field_language = field_language($entity_type, $entity, $field_name, $langcode);
  if (isset($entity->{$field_name}[$field_language])) {
    foreach ($entity->{$field_name}[$field_language] as $delta => $file) {
      $existing_files[$file['fid']] = $file;
    }
  }

  // Start creating the translated copy
  foreach ($items as $delta => &$file) {

    // keep alt, title, description if they already exist
    if (isset($existing_files[$file['fid']])) {
      foreach (array(
        'title',
        'description',
        'alt',
      ) as $property) {
        if (!empty($existing_files[$file['fid']][$property])) {
          $file[$property] = $existing_files[$file['fid']][$property];
        }
      }
    }
  }
}