Dynamic image path with token for drupal

Currently I use FCKEditor and IMCE for online article writing. It is good for article writing with both text and image content. But When I upload image in the IMCE browser interface. I can only upload the image into an existing diretory, A directory need to be created with the IMCE administration interface before the upload.

If you have only a few image to upload, this will not cause any problems, but becaues I have quite a few category of content and I 'd like to put each image under each category 's own diectory automatically, I will have to use a few more modules for implmentation of  dynamic image path placement.

The required modules

Of course Drupal token module is the basic of any dymamic path solution. We will also need the following module:

  • CCK
  • cck filefield
  • cck imagefield
  • filefield path (this is the core module for dynamic image path )

Steps for configuring the dynamic image path

  1. First we have to add one image field to the  content type we want to use using the CCK field definition interface. Add an imagefield to the  content type , the purpose of this imagefield is for storing our content images for the content.
  2. Then we will configure this imagefield for the following properties:


    Remember that imagefield is also a special kind of filefield, It has all the filefield features, we will enter '[vocab]/[term]' into the file path definition. these drupal token will be replaced at the runtime by the real vocabulary and term of the article and will ensure that our uploaded image will be stored under the defined directory automatically.

  3. Now we have to configure the imagefield display property. because CCK imagefield will be attached under the article by default, we have to change this display property at the 'Display fields' section. We will change them to 'hidden' for both the 'Teaser' and 'Full node' mode. because we don't want the uploaded image to be display under the bottom of the content.

     

Testing our new dynamic image path feature

Now we could create an new content to test our defined image right now. In the content creation page, the cck imagefield which we configure above will be listed as following:

Here we could upload all the images for this article.

Then we have to click 'Save' before the uploaded  images be moved to the right directory. You will not find the new uploaded images under the planned directory if you do not save the article first, this is because the drupal token will only be replaced by the dynamic value only after saving the content.

Finally just go to the content where these images will be inserted, use IMCE browser to find the uploaded image under the planned directory and inesrt the image into the right place of the content.

This solution is not perfect, especially we have to save the article first before we could insert the image into the content, but it does work for placing our image under a dynamically created directory!