I have several content types using the same image field. For each content type, I have enabled file (field) paths and have set a path using tokens from the content type.

This module is great for ensuring that image files get uploaded to the correct directory, but my problem is that I would like the image files to stay where they are when they were first uploaded.

For example I have a content type called "animal." If a user uploads an image while creating a node of this type, the image goes to files/animals. I then have many other content types that may want to use an animal image, but they have their own filefield path setting. I don't want my animal images to end up being scattered across many different folders in my file structure. That kind of defeats the purpose of maintaining a clean file structure!

Is there any way I can prevent this module from moving already-uploaded files? I do not have "active updating" checked.

Thank you!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Brauny created an issue. See original summary.

Brauny’s picture

Category: Feature request » Support request
Deciphered’s picture

When you say "may want to use an animal image" I assume you are using something like Media or FileField Sources. Would I be correct? If so, can you provide specifics?

If you are using one of these modules, then this is somewhat the expected behaviour, as as far as File (Field) Paths can determine, based on the way these modules and Drupal works, any piece of content referencing this file via a File field is the canonical source of that file. It is also not possible to determine if the user using something like Media or FileField Sources wants the behaviour that is currently happening or doesn't, and changing this behaviour could break many users sites.

However, it's not all doom and gloom. What you could do is create a smart token, either in code yourself, using the Custom Tokens module, or the Rule Tokens module. This token could determine where the file is to be uploaded by advanced criteria.

I hope this helps.

JonMcL’s picture

I am also puzzled by the module's instance on moving an existing file after it has been added to a second piece of content. (via filefield_sources in my situation, but same for Media browser)

any piece of content referencing this file via a File field is the canonical source of that file

Isn't the FIRST piece of content referencing the file the canonical source of that file? Secondary (and third, and fourth) pieces of content should not move the file. When you add an existing file to a second piece of content, the file is moved and the first piece of content now has the file with a new path. I think it would be great to have a setting available to allow an existing file to remain in it's current location. Or in other words, don't apply the path unless it is a new file that is being added for the first usage.

By the way, the core File/Image modules do not move an existing file when added to a second content type (when the optional directory value is populated).

JonMcL’s picture

I'm playing around with something like this being added to filefield_paths_filefield_paths_process_file

        if (!empty($settings['process_new_only']) && empty($settings['active_updating'])) {
          if (stripos($file['uri'], variable_get('filefield_paths_temp_location', 'public://filefield_paths')) === false) {
            continue;
          }
        }

And then the process_new_only setting could be added where "Active Updating" (and the others) are on the field settings form, maybe with text like "Only process file if it is new". It's sort of the opposite of "Active Updating".

Maybe there is a better way of determining new status other than checking if it is in the filefield_paths temp directory?

JonMcL’s picture

Attaching my (quickly put together) patch to allow this idea to be an optional setting. Maybe worth review and inclusion in future release?

If the process_new_only setting is enabled, the filefield_paths_filefield_paths_process_file function will skip processing if it appears that the file is not not a new upload as part of this entity save. If the file already exists somewhere outside of the filefield_path's temp location (filefield_paths_temp_location) then it will be left alone. the "Active updating" setting will override it.

Sorry, no additional tests provided.

hughworm’s picture

I fixed this by patching filefield_paths to prevent it changing the path or name of any file with any usage shown in file_usages table, unless "Active updating" is allowed.

Not sure which approach is better. #6 is probably simpler but not sure if it's 100% robust.

FiNeX’s picture

I'm experiencing the same issue. I'm also using media/media browser to reuse existing images on other image fields with different file field paths settings.

The patch #6 fixed the problem. +1 to apply :-)

amme’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work
valderama’s picture

@hughworm Your approach seems simple and thus robust. I would like to check this approach further - your changes as a starting point might help. Can you provide a patch?

hughworm’s picture

@valderama I ended up reverting my fix and using the patch from #6. Do you still want it?

valderama’s picture

@hughworm Thanks for your reply. I also tried patch from #6 but the check with the temporary folder does not work in my setup with media 2.x - for some reason. I tried to check file_usage table and skip processing if a file is used in more then one entity (including the current).

This approach works for me, as it seems.

etron770’s picture

I would recommend to add the patch #6 to the module:
Scenario:
A huge commerce store with lots of pictures.
The graphic designer has sorted the pictures by product - the drupal system by field.
If the pictures remain on the original location the graphic designer can update/sync all pictures with ftp - do a cache clear and image Cache clear - that's it. Otherwise all files must be transferred manually.
Having this feature will save a lot of manpower when updating a huge store.

cosolom’s picture

Status: Needs work » Reviewed & tested by the community

This patch works for me. In my situation I had files which placed in node by external script. And path of placed file can be differ from filefield_path settings. So when user simply rate content with fivestar widget - all files move to new location (according to filefield_path settings). Active updating is swith off. This is very unexpected behavior.