Last updated 23 October 2012. Created on 25 March 2010.
Edited by kiamlaluno, Dave Reid, pp, dsayswhat. Log in to edit this page.

Overview of Token API changes in 7.x

Available in core

The majority of the API available with token.module in Drupal 6 is now included in Drupal 7 core with a few exceptions. The Token module is not necessary to use the API, but helps fill the gap for the missing features not available in core, like a token UI browser, and token support for core module like Fields and Profile.


The big difference is that tokens can be 'chained' in Drupal 7. This makes tokens even more powerful as instead of defining each individual token available, we can access the comment's node by using [comment:node].

For example, if you wanted to get the token for a comment's node's ID, you'd use the following tokens:
Drupal 6: [comment-nid]
Drupal 7: [comment:node:nid]

Performing token replacement

Token replacement is now part of core in Drupal 7, so this functionality is not part of the Token module per se.

To replace tokens with values provided by your module, call token_replace(), passing in the string, an array of your data, and any options as necessary.

Providing tokens

The hooks for providing tokens have been renamed. hook_token_list() is now hook_token_info(), and hook_token_values() is now hook_tokens(). Both hooks also have changed parameters and different return values than their D6 equivalents.

Listing available tokens

Drupal 7 does not provide a method or element to list available tokens. This interface continues to exist is provided by the Token module for Drupal 7 via a 'Token Tree' interface. (See Token UI browser + jQuery treeTable.)

Drupal 6:

  theme('token_help', array('node'));

Drupal 7:

  // You can call the theme function directly:
  theme('token_tree', array('token_types' => array('node')));
  // Or use it in a form:
  $form['tokens'] = array(
    '#theme' => 'token_tree',
    '#token_types' => array('node'),

Upgrading your module's tokens

There is currently no upgrade path between the old token names in Drupal 6 and the new chainable-style tokens in Drupal 7. Some helper functions are being written in token.module.

Looking for support? Visit the forums, or join #drupal-support in IRC.


glennnz’s picture

These two functions have been replaced in D7 with what?


Dave Reid’s picture

They haven't really been replaced at all. token_get_values() is most related to token_generate(), but you should not have to call that yourself.

token_prepare_tokens() is removed completely as core enforces that you use brackets around token names (e.g. [token-name) rather than supporting things like %token-name.

Senior Drupal Developer for Lullabot | | @davereid

glennnz’s picture


Sorry, I don't understand. I'm trying to update auto-username for D7, see

dafeder’s picture

It's quite difficult to find information on how to use the token chaining stuff. Where is the best documentation of this?

Aparato’s picture

could someone answer this? I've been trying to find this as well (for some time). Google shows up comments like "chaining tokens is easy" .. but nowhere can i find how :)


benjifisher’s picture

See the API docs for hook_token_info(), especially the optional needs-data and type keys. That page includes sample code; the comments include further examples and a pointer to the token_example module.