What tokens are available in Drupal?

| | Comments (0) | TrackBacks (0)
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:
reviews/[title-raw]

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
users/[user-raw]
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...

0 TrackBacks

Listed below are links to blogs that reference this entry: What tokens are available in Drupal?.

TrackBack URL for this entry: http://www.bennett.f2s.com/mt/mt-tb.cgi/7

Leave a comment

About this Entry

This page contains a single entry by MarkB published on March 31, 2008 9:41 AM.

Importing Data Into Drupal CCK was the previous entry in this blog.

PHP RSS Parsers is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.