Token

Token is an optional Drupal 6 module and is so popular that it is built into Drupal 7. The Drupal 7 Standard install includes Token.

If you are a Drupal module developer, download Token from drupal.org/project/token. Token does not do anything immediately visible, instead it provides a service to your modules and makes one part of module development easier.

You sometimes want to replace text with other information. You might want to replace [example] with a drawing loaded up as an image. Your code might look for the example token then use the page node number to find the right example illustration. The Token module will find the example token for you, pass the information to your code then replace the token with your text. All you have to do is tell Token what you are looking for and the name of your function to supply the replacement text.

Drupal 6

There are a number of reasons for not using Token in Drupal 6 and one reason has gone away. When you develop your own Drupal add-on modules, you want to make them dependent only on Drupal core modules where possible. Token is not standard in Drupal 6 and is painful to use for some tasks. At the start of drupal 6, there was a sensible reason to develop modules with your own substitution. Now that Token is in Drupal 7, yout can start using Token in Drupal 6 knowing your upgrade path is safe.

Token has an overhead you do not want loaded if you are not using all the features of Token. Drupal 7 will load Token. Using Token in Drupal 6 makes the performance of your Drupal 6 site closer to Drupal 7.

Overhead

Token does add extra page processing overhead to find the tokens and replace them. Token should be faster than performing separate find and replace processes from several different modules. Token lets you decide how you want to identify tokens and the default identification is compatible with the WYSIWYG editors used with Drupal.

The bulk of Token substitution should occur during page creation and the conversion of content to HTML. The HTML version of the page is cached for anonymous users and should not carry an overhead from Token. Logged in users get a fresh page every time and will see the overhead of Token. Logged in users will see overhead from a lot of modules. The substitution overhead will, in most cases, be less than the overhead of creating the data used for the substitution.

The main point is the overhead will only be significant if one module uses Token for one substitution. When several modules perform substitution, Token should combine the substitutions in the most efficient manner and reduce the total overhead. Once you start using Token for one thing, use it for all substitutions.

Prefix and suffix

Token lets you change the format of tokens. You can use [example], [[example]], %example%, $example!, anything you like that does not conflict with HTML and other modules.

Page content

I had a problem using Token for automated page content substitution. It is easy to set up for substitutions into paths. The documentation does not cover page content replacement. I ended up using my own code for several modules because Token or the Token documentation is missing a bit.

Token provides a replacement function for text and it does nothing special. Token then provides a full service for paths. What it needs is the full service for content bodies and teasers plus an add-on module to control those tokens by content type plus an add on module to control those tokens by role.

Conclusion

Token is in Drupal 7 so start using Token. If Token does almost what you want, but not exactly what you want, consider developing an enhancement or add-on module based on Token.