FileField/ImageField: Custom Directory Structure?
Wolfey - July 3, 2009 - 04:29
Recently, I've been working with setting up FileField (and ImageField) to allow uploading to specific directories, based on the current node's taxonomy term.
However, I am limited to a directory structure based on the taxonomy terms (and their structure). I am wondering if it is possible to customize this to allow uploading to other directories instead. For example:
- If the page's field/term is "Sample" (a top-level term), the uploaded image would be placed in "
/this-directory/" (instead of "/sample/"). - If the page's field/term is "Example" (another top-level term), the uploaded image would be placed in "
/that-directory/another-directory/" (instead of "/example/"). - If the page's field/term is "Object" (a sub-term underneath "Example"), the uploaded image would be placed in "
/one-more-directory/" (instead of "/example/object/").
Would it be possible to accomplish this in a future update (or with some custom code)?

_
I can definitely think of a way to do it, but I believe at this stage it would require a small amount of custom code.
Using FileField Paths as a base, you would simply need a token that resulted in your desired path from your given taxonomy. That's where the custom code would come in, two ways to approach that would be:
1. Add a field to the taxonomy terms page where you can define your desired path, saving the value to variables table and define a token (http://drupal.org/node/307140).
2. Use the taxonomy terms synonym field and define a token (http://drupal.org/node/307140) or find a module that already exposes the synonym as a token.
Would be interested to hear how it goes.
Cheers,
Deciphered.
- http://stuar.tc/lark
Thank You; Not Sure What to Do; Try "Custom Tokens" Module?
Thank you very much for mentioning this - I'm glad to hear that it can be done =)
Unfortunately, I still don't know what to do, particularly for defining a token - though that page provides sample code, it doesn't include any examples of how to properly set it up =/
If some instructions could be supplied for this, I would greatly appreciate it =D
(Also, after you mentioned this, I looked around and found a module called Custom Tokens - would it help if I used this as well? If so, what should the sample code look like in order for it to work?)
_
Just because I'm nice, plus I enjoy the challenge, I worked it out for you.
1. Create a taxonomy vocabulary for use with your content type.
2. Create a taxonomy term inside the vocabulary.
3. Type your desired file path on the first line of the term synonym field.
4. Rinse and repeated 2-3 for as many terms as you require.
5. Using Custom Tokens, create a new token with the following settings:
- Id: [your choice]
- Description: [your choice]
- Type: Node
- PHP replacement:
$synonym = taxonomy_get_synonyms($node->taxonomy);
return $synonym[0];
6. Set the File Path for your content types upload field (FileField, ImageField, drupal Core upload, etc) to use your created token.
That's it, should work for you :)
Let me know if you have any problems.
Cheers,
Deciphered.
- http://stuar.tc/lark
Token Creation Instructions Worked; Two Minor Issues
It works! Now I can set up custom directories for file uploads! =D
I've noticed two minor issues, though:
Thank you very much for your help - I really appreciate it =)
_
The first issue should be solved simply by using the 'Retroactive updates' feature of FileField Paths. There is a feature request to add the ability to update paths when the source tokens change (like this), but I have not started implementing the feature yet.
It seems that the second issue would be related to the configuration of the File Name field of FileField Paths, which would only occur if it had been set that way manually... however with further information it's difficult to say for sure.
I would suggest that creating an issue in the FileField Paths issue queue would be a better place to continue this conversation.
Cheers,
Deciphered.
- http://stuar.tc/lark
Thank You; "Retroactive Update" Works; Issue Created
As for the first issue: thank you for mentioning that - though that setting will need to be re-enabled any time this situation occurs, it will still save some time in case something got put under the wrong term =)
As for this part:
Does this mean that a future version might be able to move the file automatically (after saving changes to a node)? If so, that's great to hear =D
As for the second issue: it is now taken care of - check out the "ImageField: Remove Timestamp from Image URL?" issue for more information. I've submitted it for ImageField rather than FileField, as only the former is affected by the query string issue - FileField doesn't append a query string to the URL of a submitted file.
Thank you for your help - I really appreciate it =)