Friday, January 13, 2012

How to programatically change a template file in drupal 6

When building modules in drupal, you often would like to change the template file of a specific hook, for example page.tpl.php.
Drupal does not have a special function to do this, but you can play with these 2 functiones:

init_theme()

and

theme_get_registry().

For this to work you need to put a code into your menu callback function.
First, we need to build the theme registry:

init_theme();

Now, you can get the theme registry in an array to modify it, like this:

$registry = theme_get_registry();

So in registry we have an array that we can modify in any way we like. For example, the array

$registry['page']

keeps the template name and the location for page.tpl.php.

If we change the location or anything else in this array now, it will take effect only in our custom module, it will not be recorded in the cache.

After we modify what we want here, we save the array by running

theme_get_registry($registry);

That's all, we now have a different template for that hook, that will take effect only on this page.

No comments:

Post a Comment