How can I add a custom handler method to a view ?

For example, i add a custom handler in hook_views_query_alter like this:

$this->view->addHandler('page_2', 'filter', 'node__field_subsidiary', 'field_subsidiary_type_value', [], 'custom_handler_subsidiary_type');

how I manage the handler logic?

bumped to the homepage by Community 50 mins ago

This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


Can you provide a little more information about what you are trying to do?

Regardless, you would need to create a subclass that extends views_handler_field (more info on that here https://api.drupal.org/api/views/handlers%21views_handler_field.inc/class/views_handler_field/7.x-3.x)

If you're trying to modify the output of an existing field, you would need to use a hook_views_data_alter (https://api.drupal.org/api/views/views.api.php/function/hook_views_data_alter/7.x-3.x) and change the handler specified in the $data array. Like so:

$data['table_name']['field_name']['field']['handler'] = 'hook_my_custom_handler_class_name';

This function hook should live in your module's hook.views.inc file within the 'views' folder in your module's root directory.

Your new handler class should be in a new file named the same as the class with .inc as the file type. That file should be added to your .info file using files[] = path_to_.inc_file

In your .module file, you would need to specify the views API:

function hook_views_api() {
  return array(
    'api' => 3,
    'path' => drupal_get_path('module', 'hook') . '/views',

If you're still struggling, I found this video tutorial very useful https://www.youtube.com/watch?v=biKxw5iEzsY

I got caught out with spelling mistakes when doing this so check all your code!

  • I have a custom handler per a field definited with: field_maps_views_data. In object handler about field_maps_views_data, in method query() i want influence anhoter field programmatily, when i thinked: add a custom handler how: $this->view->addHandler The result I want and get: I want to add a method to manage the field A with the field B. All in the custom filter handler of field B I talk about Drupal 8 – BOES Mar 2 '17 at 17:20
  • Sorry, I have just realised this is for D8 and I haven't yet tried it – Josh Brown Mar 5 '17 at 23:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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