Hi,
I know this feature request has been made previously (#177639: Token expansion: [nid] and other node information), but I want to revisit it.
To describe the feature for those who don't want to read the previous issue, I want to use node tokens in the filepath so you could have all files for node 10 stored in 'sites/default/files/10' or something similar.
The obvious issue is that node values are unknown until the node is created.
I have started playing with some code and have come up with a working prototype, still in early stages but it does achieve the primary goal.
The way it works is as such:
- when files are uploaded during the creation or editing of a node they will be stored in the unprocessed location: 'sites/default/files/[nid]'.
- when the node is saved the files are then relocated.
Currently the only major issue I can think of is if a user references the unprocessed location of the file in any fields, as once the file is processed the location will no longer exist. My (unimplemented) solution for this is to also provide a filter which will convert the unprocessed locations.
I've attached the patch for anyone to play with, but as I said, it is still in early stages.
Any feedback or help would be appreciated.
Comments
Comment #1
decipheredI forgot to mention that this currently only works on a field with the machine name 'field_file'. This is due to my own laziness. I will change this eventually.
Also, I plan to move the majority of the code in filefield_nodeapi() to a new function in field_file.inc at some stage so other modules can also move files.
Comment #2
decipheredNew patch:
- As stated above I moved the core move function to field_file.inc
- Now works for a file field with any name
- Works with multiple file fields
- Tested with a range of node tokens
Known issues:
- Drupals core file_move() function doesn't return a meaningful value if the filename was renamed which causes issues. Will have to look into a workaround.
- File won't be moved again after there original move if any of the node tokens changed (such as [title]). May be best to stay this way, but will look into options.
Comment #3
decipheredForgot to mark this issue as a patch.
Comment #4
decipheredNew patch:
- Delete empty temporary directories after field is processed
Comment #5
decipheredNew patch:
- Fixed issue to do with moved file being renamed, turns out I was getting the new name, just didn't look hard enough.
As far as I can see this is now in a completely usable state, however it could still do with another set of eyes, testing or reviewing the patch.
I haven't addressed the issue of people referencing the unprocessed filepath or moving the file again if one of the tokens is changed, but I don't think either of these issues are show stoppers.
Comment #6
jpetso commentedfilefield_check_directory() may not be used inside field_file.inc, as that file should not depend on filefield.module. So if you need that function in there, it should be moved to field_file.inc as field_file_check_directory() (and be cleansed of filefield specific strings). You also want to move filefield_nodeapi() further up in the file, say, above filefield_file(). (The real error in your patch is the missing newline at the end of the file, I wouldn't have noticed that at all otherwise.)
Other than that, it looks quite nice to me and might work (haven't tested the patch). Approving this is dopry's call though - he brought up some arguments against node tokens in the past, and it should be up to him to decide if your patch fixes these sufficiently.
Comment #7
decipheredOk, I'll make some changes and put up a new patch, likely tomorrow.
From the previous issue the only thing I could really gather dopry was opposed to was storing a dynamic path, this method won't do that. However I will see how it plays out, while I think this is a greatly needed feature I agree that it is entirely up to dopry.
Comment #8
decipheredNew Patch:
- Made changes suggested by jpetso
Comment #9
decipheredUpdated Patch:
- Updated to 6.x-1.0-beta3
Comment #10
dopry commentedNope won't be happening... in 6.x FileFields files can be shared by multiple nodes... so which node do I use for the tokens? See the problem... Not only is the node data mutable, but now there are multiple nodes to choose from. Field tokens are out as well since files can be shared between fields. You need to try using global, user, or file specific tokens for your paths.
plus 6.x-1.x is deprecated and will not become an official release.
Comment #11
decipheredWhile I must say I'm disappointed with your decision, and slightly confused by the shared file feature (how can you upload one file in multiple places at the same time? It can only be uploaded on one node), I will accept it.
I originally looked into making this a separate module as I expected this decision, however I was unable to accomplish it. I will have to look into this again as this is a very important issue for every website I create, I believe files should always be organized in usable fashion, not dumped in one folder.
Comment #12
decipheredFunctionality moved to FileField Paths module.