In the template.php, I am using the following code for mytheme_preprocess_page().

 // Return nid for node of type "interface_configuraitons".
    $nid_config = db_select('node', 'n')
        ->fields('n', array('nid'))
        ->fields('n', array('type'))
        ->condition('n.type', 'interface_configuraitons')
    //load the configurations
    $configurationNode = node_load($nid_config);
    $interfaceConfig = array();
    $interfaceConfig['driver_url'] = $configurationNode->field_driver_url['und'][0]['value'];
    $interfaceConfig['driver_port'] = $configurationNode->field_driver_port['und'][0]['value'];
    $vars['interfaceConfig'] = $interfaceConfig;

While $interfaceConfig is initialized and available in any of the page templates, it is NULL in the html.tpl.php template. I was trying to run mytheme_preprocess_html() without success.

How do I get this preprocessing take place, so it is available in the html.tpl.php template file?

  • 1
    The preprocess_html hook is the right place. Make sure you clear your cache so the new function is registered. You also have a typo in your db_select() condition – Clive May 11 '13 at 13:12
  • the preprocess_html didn't work, but what worked was mytheme_preprocess_html... i was just not clearing the cache as pointed by Clive. Thank You Clive. ps. db_select() seems to work fine – latvian May 11 '13 at 14:00
  • 1
    Then you have a typo in the name of your content type ;) – Clive May 11 '13 at 14:30

The general rule for any THEMEHOOK.tpl.php file (where THEMEHOOK is something like 'html', 'page', 'node' etc.) is:

  1. The default variables are set in template_preprocess_THEMEHOOK() (eg. template_preprocess_html)
  2. Any module can add variables using MODULENAME_preprocess_THEMEHOOK() (eg. toolbar_preprocess_html)
  3. The active theme can add variables using THEMENAME_preprocess_THEMEHOOK() (eg. bartik_preprocess_html)

The $variables array (sometimes called $vars) is passed on from function to function in the above order, so each function can expand on or overwrite the variables added before.


A variable that is set from template_preprocess_page() or an implementation of hook_preprocess_page() made from a module or a theme is not available to the implementation of hook_preprocess_html() made from a module or a theme.

A variable is available only to specific preprocess functions, which in your case are:

  • template_preprocess()
  • template_preprocess_html()
  • MODULE_preprocess()
  • MODULE_preprocess_html()
  • ENGINE_preprocess()
  • ENGINE_preprocess_html()
  • THEME_preprocess()
  • THEME_preprocess_html()
  • template_process()
  • template_process_html()
  • MODULE_process()
  • MODULE_process_html()
  • ENGINE_process()
  • ENGINE_process_html()
  • THEME_process()
  • THEME_process_html()

MODULE is the name of any enabled module, THEME is the theme currently used to render the page, and ENGINE is the name theme engine used from the theme currently used to render the page.

Notice also that all those *_preprocess() and *_process() functions don't share the variables when called for different template files.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.