I may be alone in this, but I hate having to list Views handlers in the .info file in order for the class registry to register them.

CTools has a mechanism to autoregister its plugins. Why doesn't Views have this mechanism for Handlers? Because Views doesn't know what handlers actually exist in a meaningful way.

But your module does.

<?php
/**
* Implements hook_registry_files_alter().
*/
function MYMODULE_registry_files_alter(&$files, $indexed_modules) {
 
// Auto-register all our views incs.
  // Get a list of candidate files.
 
$path = drupal_get_path('module', 'MYMODULE');
 
// Modify this for where in your module's path it keeps its files
  // Over time I have settled on this as my standard location:
 
$directory = 'plugins/views';
 
$views_incs = file_scan_directory($path . '/' . $directory, '/.*(handler|plugin).*\.inc$/', array('key' => 'name'));
  foreach (
$views_incs as $file) {
   
$files[$file->uri] = array(
     
'module' => 'MYMODULE',
     
'weight' => 0,
    );
  }
}
?>

Note: I modified the regex while posting the blog, as my original code only looks for handlers, and you probably want handlers and plugins. I think it should work anyway.

Comments

Doesn't views at least know the available plugins using hook_views_plugins() ?

It does, but it'd be confusing to register plugins and not handlers.

I am newbie. I am trying to create a view from Apache Solr server, but whenever I add some fields, I get "missing handler' error. Would this fix that issue? If yes, where should I put this code?

Thanks

It's hard to say. That error does indicate that it can't find the handler. It may be a simple registration problem. With someone else's module, though, the simplest fix is to make sure the handler is listed in the info file. If it's not, add it, then drush cc all; drush cc registry or visit the modules page and submit it.

Note: I've found that getting the registry to rebuild properly after updating the .info file usually takes a couple of tries, because .info file contents are cached in the system table.

There are other, possibly better ways to do this. The actual name of the file is irrelevant.

If you want to follow the CTools model, name all your handler files *.class.php and then use the class.php in your regex to include them.

You can then use that same technique on all the classes in your module no matter what they're used for. Just register anything with the filename *.class.php and you're done.

Add new comment