Community Documentation

Listing available tokens

Last updated January 27, 2012. Created by Dave Reid on October 31, 2011.
Edited by axe312. Log in to edit this page.

To display a list of available tokens in your module or code, use the following examples.

Drupal 7

In a form:

<?php
$form
['tokens'] = array(
 
'#theme' => 'token_tree',
 
'#token_types' => array('node'), // The token types that have specific context. Can be multiple token types like 'term' and/or 'user'
 
'#global_types' => TRUE, // A boolean TRUE or FALSE whether to include 'global' context tokens like [current-user:*] or [site:*]. Defaults to TRUE.
 
'#click_insert' => TRUE, // A boolean whether to include the 'Click this token to insert in into the the focused textfield' JavaScript functionality. Defaults to TRUE.
);
?>

In a render array:

<?php
$form
['tokens'] = array(
 
'#theme' => 'token_tree',
 
'#token_types' => array('node'),
 
'#global_types' => FALSE,
 
'#click_insert' => FALSE,
);
?>

Drupal 6

In a form:

<?php
$form
['tokens'] = array(
 
'#theme' => 'token_tree',
 
'#token_types' => array('node'),
 
'#global_types' => TRUE,
 
'#click_insert' => TRUE,
);
?>

In a page output:

<?php
$output
= ...
$output .= '<h4>Available tokens:</h4>';
$output .= theme('token_tree', array('node'), TRUE, TRUE);
// The parameters to theme('token_tree'):
// 1. The theme hook ('token_tree')
// 2. The array of token types to display.
// 3. A boolean to allow the global token types (defaults to TRUE).
// 4. A boolean to allow the click-insert JavaScript functionality (defaults to TRUE).
return $output;
?>

About this page

Drupal version
Drupal 6.x, Drupal 7.x
Audience
Developers and coders

Site Building Guide

Drupal’s online documentation is © 2000-2012 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.