Illustration of Yeti character writing with a pencil.

Drupal 6 Tokens

Profile picture for user Rob
Rob Bayliss
CTO

Making tokens in Drupal 6 is super easy as long as you don’t mind making your own module.  Recently, we 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 textbox to the database on form submit. Once it completes, the form will continue on to the normal role_submit function, meaning we aren’t affecting existing functions.
  • rolenames_token_list() Generates the token name to be displayed in one of those massive token lists Drupal loves to generate. This token will only show up under ubercart’s conditional actions form when you are editing a conditional action involving a role email. That is the only place we are going to need it.
  • rolenames_token_values() This returns the value of the token when it is used. Without this function, emails will be sent with this in the subject line: “Thanks for subscribing to [readable-role-name].” This function just calls our helper function to get the rolename.
  • rolenames_get_rolename() Here, we actually query the DB to get the rolename. If a readable role name is set, we return it. If not, we look up the regular rolename and return that. This means we don’t need to set readable rolenames for each role, just the ones that need it.

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.