Working Around the Drupal 6 Theme Registry Using Theme Settings

When you start developing themes for Drupal 6 the first problem you will run into is the theme registry and, to a lesser degree, the css cache. The theme registry collects information on which theme functions, preprocess functions, and template files need to be used for each themable object as well as several theme.info files and other such content. Once it collects all of this information it throws it into the database and uses it to make page requests faster. This is all fine and dandy unless you are working on a new theme.

The problem will manifest itself when you add a new theme_hook function to template.php, add a new hook.tpl.php to the theme, or add a new theme_preprocess or theme_preprocess_hook function to your theme. By default the theme registry is cleared when you view the theme selection form in the admin page. My goal was to make the cache clear every page refresh during development. I've seen other themes implement drupal_clear_all_caches() in the template.php file with instructions to comment this line out when the site is no longer in development, but I wanted something a bit more...awesome.

To start, I set up a theme-settings.php:

<?php

function myTheme_settings($saved_settings) {
 
$defaults = array(
   
'myTheme_development' => 0,
  );
 
 
$settings = array_merge($defaults, $saved_settings);
 
 
$form['myTheme_development'] = array(
   
'#type' => 'checkbox',
   
'#title' => t('Enabled theme development mode'),
   
'#help' => t('Causes a theme registry and css cache reset on all page loads if enabled. Disable on a live site.'),
   
'#default_value' => $settings['myTheme_development'],
  );
 
  return
$form;
}
?>

Next, I added the following to template.php:

<?php

function myTheme_preprocess_page(&$variables) {
  if(
theme_get_setting('myTheme_development')) {
   
drupal_clear_css_cache();
   
drupal_rebuild_theme_registry();
  }
}
?>

Note: This works so long as myTheme_preprocess_page is defined prior to the last call to drupal_rebuild_theme_registry, which will happen when you enable the setting.

Finally, navigate to admin/build/themes/settings/themeName and click on the configure link next to your theme. You will see a check box with the title 'Enable theme development mode'. Check that thing and submit the configuration page. Now, every time you load a page, it clears the theme registry and the css cache. To learn more about adding custom theme settings, you should check out http://drupal.org/node/177868. If you are interested in learning even more about theming, http://drupal.org/theme-guide/6 is a wonderful source of information.