2

I am working on a module and want exposing some fields to Views and provide some custom filters. But I can't extend any Views classes without drawing a fatal error. My code looks something like this. It all works fine until I try to extend views_handler_field.

<?php
// $Id$
/**
 * @files
 *  Provide functionality for views.
 */
/**
 * Get some data and set some handlers.
 */
 function custom_views_data() {
      $data = array();
    $data['custom']['table']['group']  = t('custom');     
    $data['custom']['table']['join'] = array(
      'node' => array(
        'left_field' => 'nid',
        'field' => 'nid',
      ),
    );
    // Has form
    $data['custom']['is_checked'] = array(
      'title' => t('Has contest form'),
      'help' => t('Filter based on presence of a contest form. Nodes with contest forms will equal 1.'),
      'field' => array(
        'handler' => 'views_handler_field',
        'click sortable' => FALSE,
      ),
      'filter' => array(
        'handler' => 'views_handler_filter_string',
      )
    );
    return $data;
 }

 // If you remove everything below here, things work as expected

 /**
  *
  * Define custom handlers.
  */
 function custom_views_handlers() {
      return array(
        'info' => array(
          'path' => drupal_get_path('module', 'custom')
        ),
        'handlers' => array(
          'custom_handler_is_checked_field' => array(
            'parent' => 'views_handler_field'
          )
        )
      );

 }

 /**
  * This class fails with Fatal error: Class 'views_handler_field' not found 
  *
  */
class custom_handler_is_checked_field extends views_handler_field {}
1
  • Also, my research lead me to find a lot of modules had this issue at one point. I've been digging around in CCK in particular, but I can't seem to find how they fixed it.
    – Codeblind
    Mar 30, 2011 at 18:40

1 Answer 1

1

Is your class code in the same file as your hook, aka the .module or .views.inc file? That doesn't work because views_handler_field is in a separate file and only loaded when required.

You need to move it in a separate file which as the same name as your class and the ending ".inc", inside the folder that you defined in 'path'. (in your case, your module directory, for large modules with many files, it's recommended to place them in a views directory)

Then, views will automatically include your class including the file that contains views_handler_field (because you defined the parent relationship in your hook) when required.

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.