diff --git a/uuid_features.module b/uuid_features.module index 4470bab..91a036c 100644 --- a/uuid_features.module +++ b/uuid_features.module @@ -676,6 +676,28 @@ function _uuid_features_file_field_import_file(&$file, $module = NULL) { $file->uri = strtr($file->uri, array('#FILES_DIRECTORY_PATH#' => 'public:/')); } + // Keep existing file if it exists already at this uri (see also #1023254) + // Issue #1058750. + if(!empty($file->uri)) { + $query = db_select('file_managed', 'f') + ->fields('f', array('fid')) + ->condition('uri', $file->uri) + ->execute() + ->fetchCol(); + + if (!empty($query)) { + watchdog('uuid_features', 'kept existing managed file at uri "%uri"', array('%uri' => $file->uri), WATCHDOG_NOTICE); + // Load file from db and use it as source. + $db_file = file_load(array_shift($query)); + // Remove empty fid. Earlier code exported the fid with a NULL value. + // @TODO Remove in next major version. + if (empty($file->fid)) { + unset($file->fid); + } + $file = (object) ((array) $file + (array) $db_file); + } + } + // The file is referenced but not defined because it's already // contained in a file_entity include. if (!isset($file->uri) && module_exists('file_entity')) { @@ -695,26 +717,6 @@ function _uuid_features_file_field_import_file(&$file, $module = NULL) { ) ) ) { - // Keep existing file if it exists already at this uri (see also #1023254) - // Issue #1058750. - $query = db_select('file_managed', 'f') - ->fields('f', array('fid')) - ->condition('uri', $file->uri) - ->execute() - ->fetchCol(); - - if (!empty($query)) { - watchdog('uuid_features', 'kept existing managed file at uri "%uri"', array('%uri' => $file->uri), WATCHDOG_NOTICE); - // Load file from db and use it as source. - $db_file = file_load(array_shift($query)); - // Remove empty fid. Earlier code exported the fid with a NULL value. - // @TODO Remove in next major version. - if (empty($file->fid)) { - unset($file->fid); - } - $file = (object) ((array) $file + (array) $db_file); - } - $file = file_save($file); } elseif (isset($file->uuid_features_file_data)) {