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

corey.aufang’s picture

meant to add:

I'm hoping to get some feedback to tell if this is good or stupid.

Any suggestions?

merlinofchaos’s picture

Status: Needs review » Fixed

You haven't searched in the advanced help, then. =)

corey.aufang’s picture

Status: Fixed » Active

I 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:


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,
      'preprocess functions' => array(
        'template_preprocess',
        'template_preprocess_'. $views_hook,
      ),
    );
    
  }
  
  return $templates;
}

corey.aufang’s picture

Status: Active » Needs review
corey.aufang’s picture

Category: feature » support
merlinofchaos’s picture

Status: Needs review » Active

needs 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.

Grayside’s picture

Well, I've read through the advanced help. Not seeing anything that directly addresses this.

merlinofchaos’s picture

Status: Active » Closed (won't fix)

I 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?

Grayside’s picture

I'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.

kenorb’s picture

+1

kenorb@NS8210H:~/theme$ ls -al views | wc -l
90

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.

merlinofchaos’s picture

Unfortunately, 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.

kenorb’s picture

Status: Closed (won't fix) » Fixed

Great, #3 works for me, thank you.

/**
 * Implementation of hook_theme()
 *
 * @return array
 */
function my_module_theme() {

    $path = drupal_get_path('module', 'my_module') . '/theme';

    $theme_hooks = array(
// some other my hooks
        ),
    );
    $theme_hooks = array_merge($theme_hooks, _module_views_templates($path));
    return $theme_hooks;
}

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

juanjo_vlc’s picture

Hi, 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:

function mytoolbox_theme(){
$res= db_query("select filename from system where type = 'module' and name like 'my_features_%' and status=1");
  $templates=array();
  while ($module = db_fetch_object($res)){
    $templates = array_merge($templates,(array)_module_views_templates(dirname($module->filename)."/templates"));
  }
  return $templates;
}
carlmcdade’s picture

What would the equivalent code be in Drupal 7?

perfectcu.be’s picture

Thanks,
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.)