2

How can you implement custom filter handler for an existing field in Views 3? With hook_views_data() you can expose a new table to views but how can you alter the handler for an existing field?

1 Answer 1

4

To add a Drupal 7 views custom filter handler for an existing field, you should implement the following hooks.

//in yourmodule.module
/**
 * Implements hook_views_api().
 */
function yourmodule_views_api() {
  return array(
    'api' => 3,
  );
}

// in yourmodule.views.inc
/**
 * Implements hook_field_views_data_alter().
 */    
function yourmodule_field_views_data_alter(&$result, $field, $module) {
  if($field['field_name'] == 'field_your_existing_field'){
    $name = $field['field_name'];
    foreach ($result as $table_name => $table_data) {
      if (isset($table_data[$field['field_name']]['field'])) {
        $result[$table_name][$field['field_name'] . '_value']['filter']['handler'] = 'your_custom_handler_in_separate_inc_file';
      }
    }
  }
}

//your custom handler class in separate file
class your_custom_handler_in_separate_inc_file extends existing_handler_class{
// your code goes here
}

//Don't forget to add your_custom_handler_in_separate_inc_file.inc file in your module's .info file like.

files[] = your_custom_handler_in_separate_inc_file.inc
1
  • I guess we can also change the field's data group to make it appear in a more explicit section under the views'UI. Thanks !
    – B2F
    Commented Oct 21, 2014 at 19:54

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.