You are here
Drupal 7 - Making Your Own Tokens
According to the Drupal API:
Tokens are placeholders that can be put into text by using the syntax [type:token], where type is the machine-readable name of a token type, and token is the machine-readable name of a token within this group.
Many modules in and out of the Drupal core provide tokens which can be used in conjunction with token_replace() to provide myriad options for contextual data to be used in content on your sites. There are two hooks needed to implement your own tokens, the first one is the hook_token_info() function:
/**
* Implements hook_token_info().
*/
function simple_tokens_token_info() {
$types['andy_pangus'] = array(
'name' => t("Andy Pangus Tokens"),
'description' => t("Tokens for Andy Pangus."),
);
// Andy Pangus specific tokens.
$andy_pangus['user_name'] = array(
'name' => t("User Name"),
'description' => t("The {user}.name of the user viewing the content."),
);
$andy_pangus['user_email'] = array(
'name' => t("User Email"),
'description' => t("The {user}.mail of the user viewing the content."),
);
return array(
'types' => $types,
'tokens' => array(
'andy_pangus' => $andy_pangus,
),
);
}
The above hook is used to provide a list of types and tokens to be displayed on text editing screens, so that people editing text can see what tokens are available. We define only one type, 'andy_pangus', which will be used to replace the 'type' of [type:token]. Then we define two tokens for use along with our token type and return that information as an associative array. Next, the hook_tokens() function is used to do the actual replacement of tokens when token_replace() is called:
/**
* Implements hook_tokens().
*/
function simple_tokens_tokens($type, $tokens, array $data = array(), array $options = array()) {
global $user;
$replacements = array();
if ($type == 'andy_pangus') {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'user_name':
$replacements[$original] = $user->name;
break;
case 'user_email':
$replacements[$original] = $user->mail;
break;
}
}
}
return $replacements;
}
The hook_tokens() function is invoked on each token-type group. This allows us to provide replacement text for any of the tokens in the groups that we defined with hook_token_info(). We pass each token of our type through a switch statement which handles replacing the token with its string representation. Lastly here's the module.info file:
name = Simple Tokens
description = A module with a couple tokens
package = Andy Pangus Examples
core = 7.x
dependencies[] = token
files[] = simple_tokens.module
After you enable the module you may need to clear your cache to make the new tokens available. If you want to make sure they are ready for use, go to 'admin/help/token' to see them listed:
To test, create a new node, I used a basic page, and set the text area to use the PHP filter to render text. We'll use token_replace() to render the tokens on the page:
<p>
<strong>Your user name:</strong> <?php print_r(token_replace('[andy_pangus:user_name]')); ?>
<br/>
<strong>Your email address:</strong> <?php print_r(token_replace('[andy_pangus:user_email]')); ?>
</p>
Now save your node and check it out. Any time someone visits that page they will see the user name and email they have listed in their user information. While these tokens are very basic there are myriad possibilities which can be as complicated as you need them to be.
Download the source for this example.
- Add new comment
- 10 comments


Finally!
Dude I love you. I've been trying to wrap my brain around tokens in D7 and this article has shown me the light.
Thanks and Cheers!
Glad to help!
Glad to help!
thank you
muchas gracis for that, helps me developing a contrib module i am xdebugging at the moment.
------------------------
My Textures
another interesting source
another intresting source http://data.agaric.com/tokens-drupal-7-session-jeff-eaton
Merci. You saved me. Very
Merci. You saved me. Very good article and to the point.
Excellent article, works
Excellent article, works perfectly!
thank you
So happy when I find a great example like this online.. perfectly understandable and usable; and far more easy to understand than the API entry ;). Thanks so much for putting this online!
But is there a problem with this?
Part of token is that you can put [andy_pangus:user_name] in the text and have it replaced automatically ... not with php code.
When I did this it replaced the text between the [] but didn't remove the brackets. So either there is a problem with token or something odd with this example.
doesn't work?
When using these new tokens (in the classical way), I get the following:
Notice: Undefined property: stdClass::$name in simple_tokens_tokens() (line 42 of /var/www/mywebsite/sites/all/modules/andy_pangus/simple_tokens.module).
Also, the token is not replaced. So, it doesn't seem to work. :(
V2 Cigs-Enjoy Smoking With Various Types
I have become aware of Smokeless cigarettes, however when I saw Hollywood took a puff and could not put it down! According to e cigarette evaluations, ever since Johnny Depp was illustrated smoking a smokeless cigarette in "The Tourist", this tool have actually come to be the most recent popular not only in Hollywood, however everywhere.
v2 cig coupon deals all the batteries that have their starter sets in various lengths, with the choice of a button. The shade choices of batteries supplied by V2 Cigs are back and white, and now they offer a brand-new silver version. TheyV2 Cigs have actually constantly made an one of a kind looking e cigarette, I am thrilled to see what the stainless steel batteries will appear like.
In conformity to electric cigarette testimonial, e cigarette offer available around unbelievably a smaller sized quantity duration and of course it expands more well-liked. Making use of simple and effective design they offer an effective yet considerably much less harmful substitute to cigarette smokers. You can wake additional details in regard to e cigarette by e cigarette evaluations. cigarettes. There are lots of brand names striking the marketplace connected along with electronic cigarettes and them a little more typical.
One even more reason e cig testimonials is generally browse is merely merely due to the fact that individuals today from all walks of lives want to make certain that you are gaining the newest updates concerning a personal ecigarette brand name, along with the often implies of the write-up, that's uncomplicated to study and fast to realize.
The author is certainly encouraged to efficiently introduce the truth regarding the lots of e-cigarette business. This specific post is actually concerning the V2 Cigs testimonial, in addition to the brand name on the whole. Check out the homepage to obtain additional practical info linked along with V2 Cigs along with various other models.
Today, electronic cigarettes are the option for the common cigarette smoking cigarettes as well as they are really substantially less damaging than the common cigarettes which have been ruining to the user.
Post new comment