Reputation: 51
I'm trying to develop a module whose output is to be themable in a way very similar to that of the Views module, but I can't seem to get it to work. I've followed Using the Theme Layer (http://drupal.org/node/933976) and searched the drupal forums to no avail.
The theme hook is defined in hook_theme as
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
And I invoke the theme hook by
print render($element);
in node--beer.tpl.php
(beer is the content type name) where $element
is a render array with #theme
array(3) {
[0] => string(19) "beerxml_hop__simcoe"
[1] => string(11) "beerxml_hop"
[2] => string(15) "beerxml_unknown"
}
The template that gets invoked is beerxml_hop
, and not beerxml_hop__simcoe
as I would have hoped. Both beerxml-hop--simcoe.tpl.php
and beerxml-unknown.tpl.php
exists in the same directory as beerxml-hop.tpl.php
and beerxml-unknown.tpl.php
gets used elsewhere in the output.
What am I missing? :)
Upvotes: 0
Views: 1228
Reputation: 81
Implementation of hook_theme_registry_alter was key in solving the issue.
Another thing very important is to avoid using '-' in the template names !
For instance, this won't work:
'beerxml-hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml-hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
It is key (as is in the question though) to:
.
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
Rendering the beerxml-hop--something.tpl.php file should then be done with:
echo theme('beerxml-hop--something', array('n' => 10));
Upvotes: 0
Reputation: 31
Drupal is not searching for templates with dynamic part inside of the module folder. You have to do it manually with a few lines of code:
/**
* Implements hook_theme_registry_alter().
*/
function MY_MODULE_theme_registry_alter(&$registry) {
$path = drupal_get_path('module', 'MY_MODULE') . '/subfolder/with/templates';
$registry += drupal_find_theme_templates($registry, '.tpl.php', $path);
}
However, this trick has some limitations:
Upvotes: 3
Reputation: 395
Your pattern must match your first $element['#theme']
option
You may try
'beerxml_hop' => array (
'template' => 'beerxml-hop',
'render element' => 'beerxml',
'pattern' => 'beerxml_hop__[a-z]+',
'path' => drupal_get_path('module', 'beerxml_formatter') . '/templates',
)
Upvotes: 0