What tokens are available in Drupal?

Posted on: 31 March 2008

Drupal has a very powerful module called Tokens, which makes various pieces of data available in other modules. As an example, the Pathauto module will create friendly URLs for any piece of content based on some feature of that content, using token substitution. By default, a new piece of content will be available at the URL http://www.mysite.com/node/5 (assuming you have enabled clean URLs). However, you can create a pathauto rule which creates an alias to make that content available at http://www.mysite.com/reviews/run_fatboy_run - much more SEO-friendly.

This pathauto rule looks like this:

So far, so good. However, what happens when you want to use some feature of the content other than the title? What other data is available?

It turns out this is pretty easy, once you know where to look. In the modules/token directory is an include file for each module which exposes tokens. I wanted to use the user name as a token, so checked in token_user.inc, and found

$values['user-raw'] = $account->uid ? $account->name : variable_get('anonymous', 'Anonymous');

so I've now created a pathauto rule on user account page paths of
so every user gets a nice URL to their account page, like http://www.mysite.com/users/bob

Incidentally, the token_user.inc file also includes a bunch of help text in the overridden hook_token_list function, so it looks as though there is support for displaying this information somewhere in the Drupal admin interface. I can't find it anywhere though, and can't find any documentation around it (other than in the API.txt file to say "This function is used to provide help and inline documentation for all of the possible replacement tokens.", but no clue as to where this help and inline documentation may be). All answers on a postcard please...