By esnyder on
I have the tokens [user:mail] and [date:long] in a text field of a node. The tokens are not being replaced with any text but rather just appear as the token.
This is Drupal 7 and Tokens comes in core but I installed Tokens module and that did not resolve the issue. I also tried placing the tokens in a block but no joy there either.
Any ideas?
Comments
After researching all week
After researching all week this is what I have learned. Tokens is an API only that gives a module writer or themer the ability to use tokens in their code. Tokens are not set up out of the box (at least in D7) to be used inside blocks or nodes automatically. This means that if you place a token in your field value (a body of an article for instance) it will not get replaced with a standard version of Drupal 7. You actually have to implement the token_replace() function to make Drupal replace tokens with their strings.
What I did was create a custom region.tpl.php file and changed one line. The original file looked like this:
I changed this line to include the token_replace:
When the region areas get rendered the token_replace function takes the content and replaces all the tokens with their values.
In it's simplest form token_replace() takes a variable, replaces all the tokens in the variable and returns the new value. It can do more, look at the documentation on it here.
I have also found that you can add your own custom tokens. There are a number of standard groups of tokens that comes with the token module (now included with Drupal core as of D7). To see these install Token and then click on the help link. You will see a list of currently available tokens. You can also add your own group using some code that looks like this. Warning! This code is not production ready and may not adhere to the Drupal coding standards!
To 'Register' your custom token group in the list of tokens:
You may read about the hook token_info here.
After you have 'registered' the token group and token it still does not have a value. You need to give it a value using the next function:
You may read about the hook_tokens function here.
Now if you use these two functions in a module along with the token_replace function above in the region.tpl.php in a theme then the token [testing:name] will get replaced with "Fred the Red" anytime that token appears in a region's content.
Mail tokens are no replaced.
In registration mail for admin approval user tokens are not replacing.Is this a drupal core problem?
I am simply registering as the user to my site and getting the mail with the tokens that are not replaced with the values.
tokens not getting replaced in email
I am experiencing the same thing, maybe I'll have to try that code at the top of this thred.