I created a module to provide a special *page* template for a content type, namely the 'webform' content type.

When a 'webform' node is shown on its dedicated page, I want a special template,page-webform.tpl.php, to be used to output my page markup.

My module is very simple, it uses a preprocess function to add a suggestion based on the type of node shown on a node page.

Here's the code:

function iframe_contact_preprocess_page(&$variables) {
  // $variables['node'] is set only on a node page
  if (isset($variables['node']) && $variables['node']->type == 'webform') {
    $variables['template_file'] = 'page-webform';
  }
}

My problem is that when I place the template file inside the module folder it is not seen by the theme engine. The template is seen (and used) only when I place it in my active theme folder.

Is there anything I am missing here?

Thanks in advance.

Comments

basicmagic.net’s picture

subscribe

Drupal samurai for hire, based in Buffalo, New York, USA.
15+ years Drupal, 20+ years web.
http://basicmagic.net

herbieh’s picture

Have you tried hook_theme_registry_alter() ?
A little googling found a few articles. This one by michelle is pretty good.
http://shellmultimedia.com/articles/hookthemeregistryalter-advanced-temp...

Chill35’s picture

Hey thanks a lot!

This is *exactly* what I was needing. Except..

function MODULENAME_theme_registry_alter($theme_registry) {
  // Remove the first path under 'node' which is the one for the
  // module that created the template
  $originalpath = array_shift($theme_registry[$template]['theme paths']);
 
  // Get the path to your module
  $modulepath = drupal_get_path('module', 'MODULENAME');
 
  // Stick the original path and then your module path back on top
  array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}

Not sure if this is kosher... but I will give this code a try and report back.

Caroline
11 heavens.com

Chill35’s picture

$theme_registry is not passed by reference, so any changes made to that variable will be lost.

$template has not been set, it needs to be set to 'page' in my case.

And when I correct these, it works!

Here is the corrected code (replace capitals with what is relevant in your situation):

function MODULE_NAME_theme_registry_alter(&$theme_registry) {
  $template = 'THE_THEME_HOOK_HERE'; // example: page
  // dsm ($theme_registry);
  $originalpath = array_shift($theme_registry[$template]['theme paths']);
  // Get the path to this module
  $modulepath = drupal_get_path('module', 'MODULE_NAME');
  // Stick the original path with the module path back on top
  array_unshift($theme_registry[$template]['theme paths'], $originalpath, $modulepath);
}

Caroline
11 heavens.com

Chill35’s picture

function MODULE_NAME_theme_registry_alter(&$theme_registry) {
  $template = 'THE_THEME_HOOK_HERE'; // example: 'page'
  // Get the path to this module
  $modulepath = drupal_get_path('module', 'MODULE_NAME');
  // Stick the original paths with the module path back in the array
  array_unshift($theme_registry[$template]['theme paths'], $modulepath);
}

This code can even be more succinct...

There are issues of performance here probably though, that hook is always called...

I decided to blog about this: http://11heavens.com/theming-Drupal-6-from-the-module-layer

Caroline
11 heavens.com

dwees’s picture

I'd like to do the same basic thing in Drupal 5. Theme a particular node type a specific way, without relying on the user to move any code over into their theme folder.

Is this possible in Drupal 5 at the module level? I've noticed, for instance, that the panels module disables blocks on certain admin pages, which seems very useful to me. Actually I want to disable all content for the node/add/mynodetype pages, except what my module produces.

Dave

My site: http://www.unitorganizer.com/myblog

Chill35’s picture

I would like to know this as well.

Caroline
11 heavens.com

dwees’s picture

This is supposed to be possible using a hack. Basically you create a function, call it phptemplate_page and use that function to theme the node appropriately, but this seems to me to creating the problem that a user can destroy my module's functionality just by wanting to use the same function in their template.php file.

I'm hopeful that there is another way?

Dave

My site: http://www.unitorganizer.com/myblog

Chill35’s picture

I wanted to add: that hook is *not* always called, it gets called when the theme registry is rebuilt (ie: when cache is cleared).

Caroline
11 heavens.com

wrightnow’s picture

I've posted a forum issue that's very much related, since this technique doesn't seem to be applicable in 6.20. Any thoughts?

http://drupal.org/node/1105922

wrightnow’s picture

Forum contains the resolution for this issue, much thanks. http://drupal.org/node/1105922