简体   繁体   中英

Drupal 7 Dynamic Theming

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? :)

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:

  • You can't use separate preprocesses for templates, only the base preprocess will be launched.
  • Extension of template files is hard-coded.

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:

  • use '_' in the theme hook name ('beerxml_hop') and pattern ('beerxml_hop__')
  • but '-' in the 'template' argument ('beerxml-hop') and template filename ('beerxml-hop--something.tpl.php')

.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM