Overriding Views Handlers in Drupal 7

a.abuzakham

I’m sure we’ve all been here, having a problem with Views and having the need to override them; a common situation where I face this, is while using special case Views with “i18n”, for that, here are the five steps I use to override Views.

Step #1: Create a Custom Module & Two Files

includes/views/YOUR_MODULE.views.inc
includes/views/handlers/YOUR_CUSTOM_CLASS.inc

 

Step #2: In YOUR_MODULE.module File, Register View API Information:

<?php
function YOUR_MODULE_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module',
 'YOUR_MODULE') . '/includes/views',
  );
}
?>

 

Step #3: In YOUR_MODULE.info, Include The Files:

files[] = includes/views/handlers/
YOUR_CUSTOM_CLASS.inc
files[] = includes/views/YOUR_MODULE.views.inc

 

Step #4: In YOUR_MODULE.views.inc, Alter Table Structure in YOUR_MODULE.views.inc:

<?php
function YOUR_MODULE_views_data_alter(&$data) {
  $data['taxonomy_term_data']['tid_representative']
['relationship']['handler'] = 'YOUR_CUSTOM_CLASS';
}
?>

 

Step #5: Create Your Custom Class to Override View Handler Class in File YOUR_CUSTOM_CLASS.inc & Extends With Old Class (Inheritance):

<?php
class YOUR_CUSTOM_CLASS extends
 views_handler_OLD_CLASS {
  //override your function
}
?>