Is there a way to store the token replacement values of tokens put in a form field in the database rather than the tokens themselves?

I have a text field in a custom content type that has a token as default value (using Token Filter), [current-user:uid], so on node creation the replacement value is inserted into the node. In the database, it's stored as '[current-user:uid]', rather than as the replacement value. What is one way of storing the replacement value rather than the token string in that table?

  • Your question is so generic that I can only answer it with an equally generic: "Yes, there is a way to do that".
    – Letharion
    Feb 17 '12 at 12:31
  • If there is a way to do that, would you mind giving me a hint what that way is?
    – hclayton
    Feb 18 '12 at 23:55

The gist of it is to execute token_replace in a suitable location. Here's an example.

hook_node_presave($node) {
  if ($node->type == 'page') {
    $replaced_text =  token_replace($node->field_with_replacements['und'][0]['value']);
    $node->field_with_replacements['und'][0]['value'] = $replaced_text;
  • hook_node_presave plus token_replace worked like a charm on various fields, all but the Serial module field (which has a different sort of array, Serial also has problems with Pathauto). Many thanks.
    – hclayton
    Feb 25 '12 at 2:09
  • Yes, serialized data would need to be un-serialized before tokens could be replaced. If you consider this answer "right", you should accept it as the right one.
    – Letharion
    Feb 25 '12 at 20:44

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.