Token Module: How I Learned To Use the Token API and Stop Re-Implementing Dynamic String Replacement

Greg's picture

Greg Knaddison

Growing Venture Solutions

July 2008

DrupalCamp Colorado

What are Tokens?
Little bits of text that get replaced with other bits of text.

Have you used:
ecommerce like ec_mail Custom Breadcrumbs Organic Groups - og Automatic Nodetitles (aka auto_nodetitle) Pathauto Upload Path PressFlow Placement Ubercart Invite Tokenize Audio Workflow-ng CCK Taxonomy CCK Full Name CCK Taxonomy SSU (Super Select Ultra) Fieldset Menus Node Relativity Backup and Migrate User stats Simplefeed Page title Token Node Tokens (TNT) Temporary Invitation Activity Type-local nids FileField Actions Token Email for 5.x
Me too.

If you haven't...here's a quick demo.

Token for Developers:
// Consuming tokens
// [user-name] - The name of the currently logged in user.
$original = 'Hello [user-name]';
drupal_set_message(token_replace($original, $type = 'global', $object = NULL,
$leading = '[', $trailing = ']')
// Prints "Hello greggles"

Providing Your Own Tokens
hook_token_list()
hook_token_values()

Questions? Ideas For Improvement? Bugs?


Featured Team Member

Evelyn is constantly exploring design and usability in her daily life and learning how we can make this a prettier, more usable world.

We Wrote the Book On Drupal Security:

Cracking Drupal Book Cover

We'll be at Drupalcon San Francisco

DCSF Badge
We're sponsoring, attending, and teaching at Drupalcon San Francisco. We hope you'll join us.