I have searched fairly extensively for a simple way to have views templates in a module and couldn't find anything.
I've created a function that can be used in conjunction with hook_theme.
The function takes a directory that you provide and scans for files matching a views template file name.
Based on the template type, it created a correct hook_theme entry for each file found.
Once it has processed all files, it returns the data.
function module_theme($existing, $type, $theme, $path) {
$templates = array();
$templates += _module_views_templates($path .'/templates');
return $templates;
}
function _module_views_templates($templates_path) {
$templates = array();
$views_templates_types = views_theme();
$files = file_scan_directory($templates_path, '^views.*\.tpl\.php$');
foreach ($files as $file) {
$template_name = str_replace('.tpl.php', '', $file->basename);
$template_info = explode('--', $template_name);
$views_hook = str_replace('-', '_', $template_info[0]);
$file_path = str_replace('/'. $file->basename, '', $file->filename);
$templates[str_replace('-', '_', $template_name)] = array(
'arguments' => $views_templates_types[$views_hook]['arguments'],
'path' => $file_path,
'template' => $template_name,
'original hook' => $views_hook,
);
}
return $templates;
}
Comments
Comment #1
corey.aufang commentedmeant to add:
I'm hoping to get some feedback to tell if this is good or stupid.
Any suggestions?
Comment #2
merlinofchaos commentedYou haven't searched in the advanced help, then. =)
Comment #3
corey.aufang commentedI read the section titled "Theming your views in your module".
I do a lot of development using views and have to theme the individual parts alot. Some views have 4 or more template files and a project can have 20 or more views.
Manually creating a hook_theme() entry per field would be extremely time consuming, especially if there changes to a view.
The point of this function is that a hook_theme() entry is created for each file automatically.
I admit that I overlooked the section about listing the preprocess functions, so I have attached the corrected function below:
Comment #4
corey.aufang commentedComment #5
corey.aufang commentedComment #6
merlinofchaos commentedneeds review means there's a patch attached; there isn't one here.
I don't know if this is a good idea or not, honestly. It'll probably take someone with some experience putting views templates in modules to determine whether or not it is.
Comment #7
Grayside commentedWell, I've read through the advanced help. Not seeing anything that directly addresses this.
Comment #8
merlinofchaos commentedI really don't know what we're expected to do here. I already said I don't know if this code is a good idea or not. I guess if you put it in your module and it works and you like it, great. What am I supposed to say?
Comment #9
Grayside commentedI'm just responding to the pointer to look at the advanced help. I'm still learning my way around the views hooks, and if the documentation there was clear enough to explain how to tuck templates into a modules directory, then either I have much experimenting to do, or it is much more complicated than I would have expected. Either way, this thread is one of the only pieces of documentation on the subject I could find on d.o.
Comment #10
kenorb commented+1
Sorry, but if you have 100 tpl files in one views directory, it's not possible to find anyone right. In my opinion it should be implemented as a feature.
Comment #11
merlinofchaos commentedUnfortunately, what's in the advanced help is all I have on trying to do Views theming from within a module. Most modules don't seem to need it, so it doesn't come up that often. The theme system isn't really *designed* to let module A theme for module B, so we came up with a solution and documented what we knew about it. Taking that the next step is not something anyone has done and reported back on.
Comment #12
kenorb commentedGreat, #3 works for me, thank you.
Comment #14
juanjo_vlc commentedHi, I've just tried the code from #3 and It works great, but putting it on every module you make with default views is a waste of code, I think it would be better to place it on a oneforall module and call it from the oneforall_theme hook.
My modules are developed as features, so usually I get mymodule.module overwritten, but all my names follow a pattern, so I do this:
Comment #15
carlmcdade commentedWhat would the equivalent code be in Drupal 7?
Comment #16
perfectcu.be commentedThanks,
This saved me a lot of headache. The only problem I had, is with the code in comment #3, I had to add some placeholder values to this line:
$views_templates_types = views_theme();views_theme expects four arguments and will throw errors if they are not there, but just adding empty strings for each worked fine for me. (I'm running views 6-3.x, so maybe this wasn't a problem with the version of views you wrote this for.)