Drupal 7 hook_theme() not loading template file

丶灬走出姿态 提交于 2019-12-07 13:08:55

问题


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.


回答1:


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'
    ), 
  );  
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/7884117/drupal-7-hook-theme-not-loading-template-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!