Community Documentation

Token API changes for Drupal 7

Last updated October 23, 2012. Created by Dave Reid on March 25, 2010.
Edited by kiamlaluno, 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.

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:

<?php
  theme
('token_help', array('node'));
?>

Drupal 7:

<?php
 
// 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.

Comments

These two functions have been

These two functions have been replaced in D7 with what?

token_get_values()
token_prepare_tokens()

Glenn
Vicus

They haven't really been

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.

Dave Sorry, I don't

Dave

Sorry, I don't understand. I'm trying to update auto-username for D7, see http://drupal.org/node/1107702

Glenn
Vicus

Page status

About this page

Drupal version
Drupal 7.x
Audience
Programmers, Site administrators

Site Building Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here