How to speed up Drupal by altering the tokens UI

Posted by on

On larger Drupal sites, the Token module quickly becomes unwieldy, and the UI can consume precious resources while often being never used. Here are some options for mitigation of this.

Token_tweaks module

Written by the same guy who wrote token, token_tweaks aims to reduce the recursiveness of the theme_token_tree() function. This simple fix is enough on most sites to speed up page loads, and reduce memory consumption.

This is a real life example from a current (large) Drupal project I am working on. With vanilla token module installed, and viewing an admin page with a theme_token_tree() rendered on it (it happens to be a webform configuration page). I used devel to produce these metrics.

Page execution time was 2850.96 ms. Memory used at: devel_boot()=9.4 MB, devel_shutdown()=116.07 MB, PHP peak=163.25 MB.

And there was one really insane database query happening (yes that is 244ms!):

After token_tweaks

Page execution time was 848.18 ms. Memory used at: devel_boot()=8.03 MB, devel_shutdown()=76.9 MB, PHP peak=77.75 MB.

And now the query is much more manageable (0.64ms!):

How to install token_tweaks

drush dl token_tweaks
drush en token_tweaks -y
drush vset token_tree_recursion_limit 1

The above code will set the recursion limit to only 1 level deep. This is the minimum allowed.

Patching contrib to support the new dialog that renders the tokens

Recently there was a commit that introduced a new theme function for rendering the the token_tree using theme_token_tree_link(). This essentially stops rendering the jQuery table expand fieldset, and replaces it with an on-demand modal dialog. The beautiful part is that you only need to include one additional parameter in the form element, in the case of the webform module, the token_tree form API element went from

$fieldset['token_tree'] = array(
  '#theme' => 'token_tree',
  '#token_types' => $groups,


$fieldset['token_tree'] = array(
  '#theme' => 'token_tree',
  '#dialog' => TRUE,
  '#token_types' => $groups,

The new token dialog UI

Here is a few screenshots of the new token dialog UI in action:

And when you click on the "Browse available tokens" link, this appears

There is a patch for webform that is listed at - hopefully contrib will start to adopt this shortly.

The best part about this is that the token_tweaks module and the new dialog work in tandem with each other, and together they will help reduce your Drupal memory footprint, and speed up the administration side of your site.

Forcing all contrib to use the new theme_token_tree_link() function

UPDATE 18th January 2013: Say you do want to go through the arduous task of patching all contrib that has integration with token, there is a simplier option for you. You can instead create a custom module to override the the theme implementation of theme_token_tree() all together.

 * Implements hook_theme_registry_alter().
function MYMODULE_theme_registry_alter(&$theme_registry) {
  // Prevent theme('token_tree') from generating ridiculous amounts of
  // inline markup.
  $theme_registry['token_tree']['function'] = 'MYMODULE_theme_token_tree';

 * By default theme('token_tree') renders ridiculous amounts of
 * inline markup. We can prevent this by forcing it to always
 * use its friendlier 'dialog' option (which instead outputs a
 * link to a separate page with all the markup).
 * n.b. If the path starts with 'token/tree' then we are requesting
 * that separate page, and must render the full tree.
 * @see MYMODULE_theme_registry_alter().
function MYMODULE_theme_token_tree(&$variables) {
  if (arg(0) != 'token' || arg(1) != 'tree') {
    $variables['dialog'] = TRUE;
  return theme_token_tree($variables);

As you can see this function adds in the #dialog parameter automatically, thus forcing the new dialog everywhere.

On the horizon

There is some movement with Drupal 8, to do a better job with this

Is there any other Token tips or patches that you know of? Leave me a comment.