I am trying to get a Views (2) handler to work and I don't have enough hair left to continue pulling it out.
In my module file: /** * Implements hook_views_api(). */ function mymodule_views_api() { return array( 'api' => '2.0', 'path' => drupal_get_path('module', 'mymodule') /*. '/views'*/, ); }
/** * Implements hook_views_handlers(). */ function mymodule_views_handlers() { return array('handlers' => array('mymodule_views_handler_field_age_flag')); }
In mymodule_views.inc (same directory as .module): /** * Implementation of hook_views_data_alter(). * Add "NEW" flag to nodes that are 30 days or less old. */ function mymodule_views_data_alter(&$data) { // Make sure field names are unique or they may override // standard node fields. $data['node']['age_flag'] = array( 'title' => t('Age flag'), 'help' => t('Flag to indicate the age of a Node.'), 'field' => array( 'handler' => 'mymodule_views_handler_field_age_flag', 'group' => 'Node', 'click sortable' => FALSE, ), ); }
So far, so good. The field does show up as selectable. But it keeps saying that the node > age_flag handler cannot be found".
I have added the file to the module's .info files[] - no help. I have tried placing the handler in the same directory as the module, and both an "includes" and "views" directories. No help.
The handler file itself: /** * class definition. */ class mymodule_views_handler_field_age_flag extends views_handler_field { /** * Render function: return html output * Including edit and delete action */ function render($values) { // See if the created date is less than 30 days ago. $ret = NULL; if ($values->created - REQUEST_TIME < 2592000) { $ret = 'NEW'; }
return $ret; } /** * class query. */ function query() { // Do nothing, leave query blank, we render the contents } }
What am I doing stupid that makes Views not find this handler? And yes, I am clearing the Views cache and the Drupal caches every time I blink my eyes.
Nancy
Injustice anywhere is a threat to justice everywhere. -- Dr. Martin L. King, Jr.
Your hook_views_handlers implementation is missing a few details:
1) It's missing the info => path key which tells views where to find the files that contain your handlers so that it can load them when required.
2) It's missing the parent option that tells views which other classes you're handler depends on so that those files can be loaded form disk as well (in your case it should list views_handler_field as the parent, since that is what your handler class extends).
3) This shouldn't matter much, but it is convention to place the hook_views_handlers in the same MYMODULE_views.inc file as your hook_views_data() and hook_views_data_alter().
There is a good example of how a Views 2.x style hook_views_handlers() implementation should look at http://drupalcode.org/project/views.git/blob/refs/heads/6.x-2.x:/modules/nod....
-Mike __________________ Michael Prasuhn 503.512.0822 office http://mikeyp.net