6

Since there is not support for fields in Drupal 7 tokens, how do I create the code to replace a token in an image field file path?

EDIT: First, this is with the Node Reference URL widget so the NID of the referenced node is loaded into the form array as long as the link was clicked on the referenced node.

I realize that there isn't any context to load the information to pass to the token module because there isn't any information about the node by the time token_replace() is called. This is before the NID of the referenced node is added to the form.

Adding the file path by using hook_form_FORM_ID_form_alter() works, but it's not very safe. The user might not use the Node Reference URL widget to access the page in which case the node is saved with a reference while the image path didn't have access at any point for know about the NID of the referenced node. And, if the node is then edited it will change the path on creation as it will then know about the referenced node making a mess of the place.

Here is the simple code which isn't very safe. I wouldn't mind a better solution.

Update:

It turns out that the node reference URL widget can be set to return a page not found if the referenced node's URL is not in the URL, which would make this function a little safer as it forces the user to only be able to create the node if they clicked the create node link on the referenced page.

/**
*  Implements hook_form_FORM_ID_form_alter\
*/
function HOOK_form_photo_node_form_alter(&$form, &$form_state) {
  $lang = $form['language']['#value'];
  $form['field_business_photo'][$lang][0]['#upload_location'] = 'public://photo/' . $form['field_reference_business'][$lang][0]['nid']['#default_value'];
}
  • 1
    maybe filefield_paths helps? – anonym-developer Oct 15 '11 at 8:58
  • hmm... is this question still awaiting answers ? What is the question here precisely ? – Refineo Nov 5 '11 at 19:00
1

The answer is using the Fieldfield Paths module.

The FileField Paths module extends the default functionality of Drupals core Upload module, the FileField module and many other File Upload modules by adding the ability to use node tokens in destination paths and filenames.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.