diff --git a/filefield_paths.module b/filefield_paths.module index 989a9d0..ebe9b78 100644 --- a/filefield_paths.module +++ b/filefield_paths.module @@ -231,6 +231,34 @@ function filefield_paths_entity_insert($entity, $type) { } /** + * Implements hook_entity_presave(). + */ +function filefield_paths_entity_presave($entity, $type) { + if (isset($entity->original)) { + $field_types = _filefield_paths_get_field_types(); + $entity_info = entity_get_info($type); + $bundle_name = !empty($entity_info['entity keys']['bundle']) ? $entity->{$entity_info['entity keys']['bundle']} : $type; + // Go through $entity->original to see which fileifelds have already been placed in their paths + if ($entity_info['fieldable']) { + $entity->filefield_paths_processed = array(); + foreach (field_info_fields($type, $bundle_name) as $field) { + if (in_array($field['type'], array_keys($field_types))) { + $instance = field_info_instance($type, $field['field_name'], $bundle_name); + $enabled = (isset($instance['settings']['filefield_paths_enabled']) && $instance['settings']['filefield_paths_enabled']) || !isset($instance['settings']['filefield_paths_enabled']); + if ($enabled && isset($entity->{$field['field_name']})) { + foreach ($entity->original->{$field['field_name']} as $langcode => &$deltas) { + foreach ($deltas as $delta => &$file) { + $entity->filefield_paths_processed[$file['fid']] = $file['fid']; + } + } + } + } + } + } + } +} + +/** * Implements hook_entity_update(). */ function filefield_paths_entity_update($entity, $type) { diff --git a/modules/filefield_paths.inc b/modules/filefield_paths.inc index f211b8d..f07d731 100644 --- a/modules/filefield_paths.inc +++ b/modules/filefield_paths.inc @@ -48,7 +48,7 @@ function filefield_paths_filefield_paths_process_file($type, $entity, $field, $i if (isset($instance['settings']['filefield_paths'])) { $settings = $instance['settings']['filefield_paths']; foreach ($items as &$file) { - if (filefield_paths_uri_is_local($file['uri']) && ($file['timestamp'] == REQUEST_TIME || $settings['active_updating'])) { + if (filefield_paths_uri_is_local($file['uri']) && (!isset($entity->filefield_paths_processed[$file['fid']]) || $settings['active_updating'])) { $token_data = array( 'file' => (object) $file, $type => $entity