Update 8/13/11 - For creating tokens in Drupal 7, see Tokens in Drupal 7.

Making tokens in Drupal 6 is super easy as long as you don't mind making your own module. Recently, I had a client that had built a large Drupal site that used role names as the basis for some of its URLs. All the role names were URL safe, which is pretty much the opposite of being human readable. Normally, this wouldn't be a problem because role names are rarely exposed to the user, but they created newsletters, using Ubercart's Roles module to manage the subscriptions. This left the client in an awkward spot; when a visitor signed up for a newsletter they recieved an email with a subject like this:

Thanks for subscribing to really_wierd_lookingrolename

Since the rolename was the basis of URLs, changing these rolenames to the human readable version was out of the question. It would have blown the SEO page rankings for a lot of longstanding content. My solution was to create an alternate rolename in a separate table, then expose that human readable version as a token to the user. If you've never created a Drupal module before, I reccomend checking out some of the excellent resources available at Lullabot. Otherwise, here are the files you will need:

  • rolenames/rolenames.info - Basic info about our module.
  • rolenames/rolenames.install - Holds the schema and install/uninstall information
  • rolenames/rolenames.module - Everything else

I've attached the finished files at the bottom of this post. Otherwise, create the rolenames folder, then create each of these three files. The rolename folder goes into your site's /sites/all/modules folder. Now, we fill in the blanks.

rolenames.install

  • rolenames_schema() - Schema goes here. The easiest way to write a schema is to make the table in phpMyAdmin, then use the Schema module to get the schema array.
  • rolenames_install() - This function in simple. Just add: drupal_install_schema('rolenames'); That's it.
  • rolenames\_uninstall() - As you might imagine, this is the same: drupal_uninstall_schema('rolenames');

rolenames.module

  • rolenames_form_alter() - Here we add a textbox to the role edit form. Using hook_alter allows us to avoid creating a whole new form for such a simple task.
  • rolenames_role_submit() - This is the function to save our textb

That's really about it. If you want to download this module either for use on a site or to study, feel free.  I make no guarantees about the security or reliability of the module, but if you're worried about it you can always make your own modifications.