I would like to pass variables from my custom module to some of my custom themes. The most tutorials are concerning using hook_theme function but as I understand, it works only for creating new theme functions (new tpl files) inside module, where the hook_theme is defined. But I would need to alter theme definition of some custom theme in /sites/all/themes folder from my custom module /sites/all/modules

In particular, I need to add a variable into thems's html.tpl.php file. I would like to use custom module to define variable, because I need to make database queries to fill variable with value, and db queries should not be done in theme.

I have tried implementing hook_theme_registry_alter. I have tried to add variable like this:

function mymodule_theme_registry_alter(&$theme_registry) {
$theme_registry['html']['variables']['test'] = 'test value';

but I got error "Undefined index: #children in template_process_html"

Can anybody help?

  • template_preprocess_html() uses #children only in $variables['page'] = $variables['page']['#children']; - it should in no way interfere with your code.
    – Mołot
    Aug 23, 2013 at 8:46
  • I would use variable_set() & variable_get(): api.drupal.org/api/drupal/includes!bootstrap.inc/function/…
    – Kevin
    Aug 23, 2013 at 9:22
  • You right that it will work, altough this isn't the best practice to use those function in that case. See @Beebee answer
    – rreiss
    Aug 23, 2013 at 9:58
  • In this case, variable_set() should not be used, since the value used by the OP is temporary, while the values saved with variable_set() are not temporary. There is also a performance issue, since variable_set() clears the cache used for persistent variables, and that is bad, for a value that could change at every page load.
    – apaderno
    Sep 26, 2013 at 14:48

1 Answer 1


For your needs, you'll need to create a custom module and implement hook_preprocess_html:

function hook_preprocess_html(&$vars) {
  // This is where you do the db query
  $myvar = _get_my_var(); 

  // After this $myvar will be available in html.tpl.php
  // where you need to print it out
  $vars['myvar'] = $myvar;
  • Would you care a word of insight why hook_theme_registry_alter wouldn't work here?, and what might trigger #children error message?
    – Mołot
    Aug 23, 2013 at 8:47
  • 1
    It would work either way, I have no idea why he's getting the error with #children. It could be some leftover code from the other things he tried. Who knows.
    – Beebee
    Aug 23, 2013 at 8:55
  • Aren't preprocess function should be used only on the theme level? (I'm asking because this is the first time I see that is should be used at the module level..) Thanks!
    – rreiss
    Aug 23, 2013 at 9:56
  • @Rotem well they usually should, but it's a doable workaround for mysterious bug we can't quite nail down... If you can guess / know how to test why sudden #children error arises, I'll gladly upvote your answer.
    – Mołot
    Aug 23, 2013 at 10:09
  • @Rotem it's true that preprocessors for template themes like html and page and node etc should be on theme level, but there's plenty of modules that use preprocessors for their own module's theme functions :) So it all kinda boils down to your use case. OP said that he wants to assign the var in module therefore...
    – Beebee
    Aug 23, 2013 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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