3

I'm trying to get a very simple module to load a template file using drupal's hook_theme(). It's pretty much as simple as you can possibly imagine.

function sectionheader_theme ( $existing, $type, $theme, $path ) {
  return array(
    'sectionheader' => array(
      'variables' => array( 'foo' => NULL ),
      'template' => 'sectionheader',
    ),
  );
}

The template is named sectionheader.tpl.php. The rest of the module is working as expected. I've cleared the Drupal cache. I've inserted a die("Debug") statement in this function, and it is being executed, but my template is simply not being called, ever. The template merely has some debug text in it so I can see that it's working, but is not visible in any view of the module.

I've done everything in every example I can find, I've even copied and pasted code directly from other modules, and this template will still not load.

Jape
  • 31
  • 1
  • 2
  • I have encountered the same problem. Like Jape, I've cleared the cache, tried everything I can and have verified the _theme function is called (in my case the _theme function is adding css file and this is appearing in the results). But, no matter what I do the template file seems to be ignored and the results are empty. Oh, BTW, I've tried renaming the template file and it has always had a different name than the module. – Bryan Jul 15 '12 at 02:21

3 Answers3

5

Note, if you have put your template file in a /theme subfolder in your module dir ( which is best practice), you'll also need to specify the file path in hook_theme

function example_theme($existing, $type, $theme, $path) {
  return array(
    'example_function' => array(
      'variables' => array('var1' => array(), 'var2' => array(), 'var3' => array()),
      'template' => 'example-template',
      'path' => drupal_get_path('module', 'example').'/theme'
    ), 
  );  
}
David Thomas
  • 4,027
  • 3
  • 28
  • 22
  • I've attempted to put the template in a sub folder, and created a path the way that you mentioned, but that didn't seem to solve the problem. – Jape Nov 02 '11 at 21:49
1

I had the same problem as yours, and I solved it by clearing the cache, I searched from the database, and in the cid column of table cache, I get something like "theme_registry:*", and I remove them, it works.

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
wfy
  • 46
  • 5
  • In general, using answers to get around the minimum rep requirement for comments is not acceptable. There are reasons the minimum is in place, and Stack Overflow has a question-and-answer format, not a discussion thread format, and "Answer" is not like "Reply" in a discussion thread, it means you're actually providing an answer to the question. Normally I'd flag an comment posted as an answer for deletion, but ironically, this one is actually a legitimate answer and shouldn't have been a comment anyway, so I just edited out the non-answer commentary. – Adi Inbar Feb 24 '14 at 04:32
0

As mentioned in the comment above I hit the same problem. Everything was working fine in a development module but when I simply copied this module into a new one that would become my production module the template file no longer worked. I tried everything mentioned above withut luck. The original module was disabled and only the new one was enabled.

I even went back to see if the original module's theme could work and it didn't. hmmmm.

When I changed the name of the theme it suddenly started to work: the template file was located and displayed.

So, it appears that any module that registers a theme name ---- even if it is disabled --- still registers a theme AND it seems that theme names need to be unique throughout the system.

Answer: look for the same theme name being declared in other modules

Bryan
  • 1,103
  • 12
  • 16