Token API changes for Drupal 7

Last updated on
30 April 2025

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.

Chaining

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.

Help improve this page

Page status: Not set

You can: