HOWTO: Custom paths per taxonomy term with FileField Paths and Custom Tokens.

Browsing through the Drupal.org support forums the other day, I came across an interesting support request, something that I just had to see if I could solve, maybe a niche issue, but something that really appealed to me.

The user was wanting to know if they could have files uploaded to specific directories based on the taxonomy term chosen, but not specifically in the same structure of the taxonomy tree or with the values of the taxonomy terms.

For example:

  • Term 'A' uploads files to directory 'X'.
  • Term 'B' uploads files to directory 'X/Y'.
  • Term 'C' uploads files to directory 'G/X'.
  • Term 'D' uploads files to directory 'P'.

As my experience with Drupal has shown, nothing is impossible, everything is achievable. And I knew with a little elbow grease this should be achievable with my FileField Paths module.

I initially suggest creating a custom module to define some custom tokens to achieve the goal, but the user, still seeking a simpler option, pointed me in the direction of a module called Custom Tokens.

The end solution adds the ability to use the mostly useless taxonomy term synonym field to define a custom upload path.

 

Notes:

  • This is written with the assumption that you have Drupal installed and setup and have a reasonable knowledge in using it.
  • Drupal 6 was used in writing this but the process should work on both Drupal 5 or 7 with minor modifications.
  • The Drupal core Upload module was used here, but any of FileField Paths supported upload modules could easily be substituted (FileField, ImageField, Image, etc).

 

Required Modules:

 

Step 1 - Install and enable modules.

  1. Download, install and enable the above required modules, for further information on this please refer to the Drupal.org handbook guide 'Installing contributed modules'.
  2. Enable the Drupal core Taxonomy and Upload modules.

 

Step 2 - Setup your Taxonomy.

  1. Create a Taxonomy Vocabulary (http://[www.yoursite.com/path/to/drupal]/admin/content/taxonomy/add/vocabulary) with the following settings:
    • Vocabulary name: Category*
    • Content types: Page*

    *settings can be modified to meet your own needs.

  2. Add a Taxonomy Term to your Vocabulary (http://[www.yoursite.com/path/to/drupal]/admin/content/taxonomy/[term id]/add/term) with the following settings:
    • Term name: Term 1*
    • Advanced Options > Synonyms: path/1*

    *settings should be modified to meet your own needs.

  3. Repeat Step 2.2 as many times as you need, using different values each time.

 

Step 3 - Create your token.

  1. Create a new token (http://[www.yoursite.com/path/to/drupal]/admin/build/tokens/create) with the following settings:
    • Token ID: token_custom_path*
    • Description: Taxonomy term custom path.*
    • Type: Node
    • PHP replacement:

      $synonym = taxonomy_get_synonyms($node-<taxonomy);
      return $synonym[0];

    *settings can be modified to meet your own needs.

 

Step 4 - Setup your Uploads.

  1. Browse to your content types settings page (eg. http://[www.yoursite.com/path/to/drupal]/admin/content/node-type/page) and set the following settings:
    • Workflow settings > Attachments: Enabled
    • Workflow settings > Upload Path settings > File Path: [token_custom_path]

 

That's it, all you have to do now is create a new node, select your desired taxonomy term and your file will be uploaded to your custom path.

One more thing to note however, if I was to be wanting this feature in one of my own sites I would ultimately opt to write a custom module that added an extra field to the taxonomy term configuration page instead of using the synonym field, and I would also define the tokens in the custom module, but this solution does achieve the same goal in a simpler manner.

Hi!
I have followed these instructions, but it does not work :-(
1. question: taxonomy_get_synonyms($node-taxonomy);
2. Let suppose I want to put the files into the
/sites/default/files/hallgato/path/1 directory

What are the correct upload path settings?
Workflow settings > Upload Path settings > File Path: [token_custom_path]

Thank You!

Tamas (not verified) on 10 August 2009 - 5:44pm
© 2007 - 2010 deciphered.net. All rights Reserved.