Jump to:
| Project: | Drupal core |
| Version: | 8.x-dev |
| Component: | theme system |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
Right now, when you implement hook_theme on a module and define the pattern attribute, only items appearing in the theme directory are picked up by the pattern. It would be useful if the same feature could be extended to modules as well.
As workarounds, packaging dynamically named template files with a module can be done in two ways.
One way is to define a template preprocessor which adds to the template_files variable. I used this workaround to fix my issue in [#400990]
<?php
function ddd_reports_theme($existing, $type, $theme, $path)
{
$classes = _ddd_reports_providers();
$hooks = array();
foreach ($classes as $class) {
$template = str_replace('_', '-', $class['class']);
$path = drupal_get_path('module', $class['#module']);
$class['arguments']['module_name'] = $class['#module'];
$class['arguments']['class_name'] = $class['class'];
$args = $class['arguments'];
$hooks[$class['class']] = array(
'arguments' => $args,
'pattern' => $class['class']."__",
'path' => $path,
'template' => $template,
'preprocess functions' => array('template_preprocess_ddd_reports'),
);
}
return $hooks;
}
/**
* The only way we could get it to search for patterns in modules
*/
function template_preprocess_ddd_reports(&$vars, $hook) {
$format = arg(2);
$report = str_replace('_', '-', $hook);
$vars['template_files'][] = "{$report}--{$format}";
$vars['vendor_path'] = "/".drupal_get_path('module', 'wfp_reports') ."/vendor";
module_load_include('inc', $vars['module_name'], $vars['class_name']);
$report_class = new $vars['class_name']();
$report_class->add_preprocess_variables($vars);
}
?>The other option would be to dynamically create all the template keys (the keys of the hook_theme array) so the theme registry knows about the template files from the start. But this option was sort of smelly, so we went with the first workaround.
I wanted to see what other people thought about this. Frankly, there could already be a 'correct' way to do this and I just missed it.
At a minimum, I think the hook_theme pattern documentation should explicitly say that it will only find files matching the pattern from the theme directory.
For some other history on this, check [#400990]
Comments
#1
http://drupal.org/node/400990
#2
subscribe
#3
You need to prefix your pattern functions with "phptemplate_" instead of "theme_". The location of the file is not relevant. I don't know if that is a feature or a bug, atleast, it's not documented.
So, this should either be correctly documented somwhere or fixed (also look for theme_ when looking for pattern theme functions)
#4
But what if you wanted to use a tpl.php file instead of a function, that is the crux of this problem for me.
#5
Hello everyone, I found a way. See http://hddigitalworks.com/provide-theme-templates-from-a-module
#6
This appears to still be an issue in D7. I haven't tested D8 but things need to be fixed there, first, so setting that as the version. This seems like a bug to me, not a feature request. If it can find the base template in the module directory, not being able to find the patterned one makes no sense.
I want to apologize in advance if this is already fixed and it's just me goofing up. This is the first time I've dug into this stuff and it's nearly 1am here so it's entirely possible I'm missing some clue. :)
Michelle