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?


Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <blockquote> <br> <p>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options

Featured Team Member

Ben enjoys working on cutting edge technologies and finding practical ways to use those tools to make better sites for clients, faster.

Drupalcamp Colorado

We had fun at Drupalcamp Colorado!

Drupalcamp Colorado

We Wrote the Book On Drupal Security:

Cracking Drupal Book Cover

We were at Drupalcon San Francisco

See the videos now: