Token Module for Drupal Adds Flexibility to Content

The Token Module for Drupal allows you create tokens for displaying text on your Drupal website.  The ability to create tokens (place holders that act as variables for programmatically derived values), adds power and flexibility to your content, while improving your SEO.

Tasks for which the application of tokens is useful:

  • Mass mailings
  • Message notifications
  • URL path generation
  • Page titles
  • Value holders in templates

In essence, tokens are tags that can be placed into larger content for live substitution. The Token module for Drupal provides the Drupal API for tokens that can be used by different modules.

It’sworth noting that the Token Module doesn’t provide any visible functionality, only token handling services for other modules.

Token Examples and Uses

Drupal tokens are available for the following categories:

  • Comments
  • Nodes
  • Taxonomy
  • Users
  • CCK
  • Node references
  • Text
  • Date
  • Global

Tokens are used by modules, and modules can also provide tokens. Here are some of the main modules that use the token API:

  • PathAuto
  • Organic groups
  • eCommerce
  • Ubercart
  • Menu Token
  • Custom Token

Tokens work on the following basic logic:

token -> replace function -> result

To illustrate: The book [node-title] was written by [author-name].

could convert to:

The book A Matter of Honor was written by Jeffrey Archer.

Menu Token

This module provides tokens in menu links. For example, you could create a menu with a path using the user ID [uid] as follows:

In this case, the menu_token.module will use the [uid] token and change the URL “on fly” to: user/2 (assuming you are user 2). Each user can be directed according to their user ID.

Using tokens in notification message

Let’s say we want to create a notification that will be sent to the author of a post when someone leaves a comment. In the example below, we use Comment Tokens, Node Tokens, and Global Tokens:

A new comment has been posted on your [type-name], [title] at [site-url]/node/[comment-nid]#comment-[comment-cid]
 
Comment information:
[comment-title]
[comment-body-raw]
Submitted by [comment-author-name] on [comment-mm]/[comment-dd]/[comment-yyyy]

The above could be translated as follows:

A new comment has been posted at your blog entry, Drupal Lessons, at site.com/content/blog/drupal_lessons/234#comment-21
 
Comment Information:
Great Article
Helped me a great deal with my Drupal learning curve. Thanks.
Submitted by Jack Gitter on 03/12/2010

Tokens in PathAuto

It’s possible to specify URL replacement patterns using the PathAuto module. You can assign a basic pattern such as [term-raw]/[title-raw]-[nid].html for all node types, which will then create a path containing the name of the top taxonomy term and the page title.

Additional Features

The Custom Tokens Module can be used to create new tokens. Using PHP code to build specific replacements can improve other modules that use the Token Module, but you’ll need a good working knowledge of PHP.

Examples of Custom Tokens include:

  • Add last 4 digits of credit card to Ubercart email
  • Create Custom Tokens based on CCK fields
  • Get the vote value of a logged-in user
  • Tag content in community posts
  • Generate dynamic image paths

Download and Installation Instructions

You can download the Token Module for Drupal here.

Extract the zip file, copy the unzipped folder to the Drupal module folder, and enable the module using the Drupal admin area. Simply navigate as follows:

Administer > Site Building > Modules

Next, enable the token as shown below and save the configuration:

No Comments

Add a Comment

Please keep your comments relevant to this blog entry. Email addresses are never displayed, but they are required to confirm your comments.