diff --git a/filefield_paths.module b/filefield_paths.module
index 989a9d0..cc06f57 100644
--- a/filefield_paths.module
+++ b/filefield_paths.module
@@ -122,12 +122,24 @@ function filefield_paths_form_alter(&$form, $form_state, $form_id) {
'#weight' => 10,
);
+ // File exists behavior. Keep disabled if the file name option is the same as the original file, as this can result in
+ // the uploaded file itself being moved onto itself and then deleted.
+ $form['instance']['settings']['filefield_paths']['replace_existing_files'] = array(
+ '#type' => 'checkbox',
+ '#title' => t('Replace existing files'),
+ '#description' => t('If a file with the same name already exists in the destination directory, replace it') . '.' .
+ '
' . t('To use this feature, you must change the "File name" option to be something other than the exact name of the uploaded file') . '.',
+ '#weight' => 11,
+ '#default_value' => isset($settings['replace_existing_files']) ? $settings['replace_existing_files'] : FALSE,
+ '#disabled' => FALSE //isset($settings['file_name']) && $settings['file_name']['value'] == '[file:ffp-name-only-original].[file:ffp-extension-original]' ? TRUE : FALSE
+ );
+
// Retroactive updates.
$form['instance']['settings']['filefield_paths']['retroactive_update'] = array(
'#type' => 'checkbox',
'#title' => t('Retroactive update'),
'#description' => t('Move and rename previously uploaded files.') . '
' . t('Warning: This feature should only be used on developmental servers or with extreme caution.') . '
',
- '#weight' => 11,
+ '#weight' => 12,
);
// Active updating.
@@ -136,7 +148,7 @@ function filefield_paths_form_alter(&$form, $form_state, $form_id) {
'#title' => t('Active updating'),
'#default_value' => isset($settings['active_updating']) ? $settings['active_updating'] : FALSE,
'#description' => t('Actively move and rename previously uploaded files as required.') . '' . t('Warning: This feature should only be used on developmental servers or with extreme caution.') . '
',
- '#weight' => 12
+ '#weight' => 13
);
}
}
@@ -254,6 +266,12 @@ function filefield_paths_entity_update($entity, $type) {
}
$files[] = &$file;
}
+ }
+
+ // Store the original entity before we modify it
+ $entity->original = $entity;
+
+ foreach ($entity->{$field['field_name']} as $langcode => $deltas) {
// Invoke hook_filefield_paths_process_file().
foreach (module_implements('filefield_paths_process_file') as $module) {
if (function_exists($function = "{$module}_filefield_paths_process_file")) {
@@ -267,7 +285,6 @@ function filefield_paths_entity_update($entity, $type) {
}
if ($processed) {
- $entity->original = isset($entity->original) ? $entity->original : NULL;
field_attach_update($type, $entity);
}
}
diff --git a/modules/filefield_paths.inc b/modules/filefield_paths.inc
index f211b8d..ebe0792 100644
--- a/modules/filefield_paths.inc
+++ b/modules/filefield_paths.inc
@@ -70,7 +70,15 @@ function filefield_paths_filefield_paths_process_file($type, $entity, $field, $i
// Finalize file if necessary.
if ($file !== $old_file) {
$dirname = drupal_dirname($file['uri']);
- if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY) && file_move((object) $old_file, $file['uri'])) {
+ $file_exists_behavior = empty($settings['replace_existing_files'])? FILE_EXISTS_RENAME : FILE_EXISTS_REPLACE;
+ if (file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) {
+
+ $moved_file = file_move((object) $old_file, $file['uri'], $file_exists_behavior);
+ if ($moved_file && $moved_file->fid != $file['fid']) {
+ $entity->{$instance['field_name']}[$entity->language][0] = (array)$moved_file;
+ file_delete((object)$old_file, TRUE);
+ }
+
// Process regular expression.
_filefield_paths_replace_path($old_file['uri'], $file['uri'], $entity);