How do I use tokens in my custom module?
bjraines - September 30, 2009 - 15:37
| Project: | Token |
| Version: | 6.x-1.12 |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
Description
I have been reading the forums and documentation.
I am creating a custom lead generation module / block / form
I would like to be able to use some tokens like node author's name and email address but I do not see how to call these up in my module.

#1
From my experience, the best way to learn is to have a look at other modules that use the token api and then adapt to your own needs. So check out the list of modules that use the token module and start reading through the code to find anything that refers to tokens.
I'll give one example from custom_breadcrumbs. To replace tokens used in breadcrumb titles and paths, determine the types of tokens (nodes, taxonomy, user, global) that can be replaced replacement, and then call token_replace_multiple(). As the name implies, this function replaces the tokens in the $titles and $path text string.
<?phpif (module_exists('token')) {
// Do token replacement.
$types = custom_breadcrumbs_token_types($objs);
$titles = token_replace_multiple($titles, $types);
$paths = token_replace_multiple($paths, $types);
}
?>
I hope this gives an idea of how to do it. There are more details that you'll get from carefully reading the code in its proper context.