Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Drupal 7 Views 3 custom field handler
dependencies[] = ctools
; Views Handlers
files[] = views/mymodule_handler_handlername.inc
<?php
/**
* Implements hook_views_api().
*/
function mymodule_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'mymodule') . '/views',
);
}
<?php
// This file must be at mymodule/views directory.
/**
* @file
* Views definitions for mymodule module.
*/
/**
* Implements hook_views_data_alter().
*/
function mymodule_views_data_alter(&$data) {
$data['node']['handlername'] = array(
'title' => t('Name of my handler'),
'help' => t('Description of my handler.'),
'field' => array(
'handler' => 'mymodule_handler_handlername',
),
);
}
<?php
// This file must be at mymodule/views directory.
/**
* @file
* Definition of mymodule_handler_handlername.
*/
/**
* Description of what my handler does.
*/
class mymodule_handler_handlername extends views_handler_field {
/**
* Add some required fields needed on render().
*/
function construct() {
parent::construct();
$this->additional_fields['field_something'] = array(
'table' => 'field_data_field_something',
'field' => 'field_something_value',
);
}
/**
* Loads additional fields.
*/
function query() {
$this->ensure_my_table();
$this->add_additional_fields();
}
/**
* Default options form.
*/
function option_definition() {
$options = parent::option_definition();
$options['option_a'] = array('default' => '');
$options['option_b'] = array('default' => '');
return $options;
}
/**
* Creates the form item for the options added.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['option_a'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $this->options['option_a'],
'#description' => t('Some description.'),
'#weight' => -10,
);
$form['option_b'] = array(
'#type' => 'textfield',
'#title' => t('Label'),
'#default_value' => $this->options['option_b'],
'#description' => t('Some description.'),
'#weight' => -9,
);
}
/**
* Renders the field handler.
*/
function render($values) {
// This ensures that even if Views changes the field name within the query, the handler will still work.
// Inspect the $this->aliases array to find the key of the field that you need.
$value = $this->get_value($values, 'field_something');
return format_string('<div class="@option_a" data-tag="@field_something" data-follow_name="@option_b"></div>', array(
'@field_something' => $value,
'@option_a' => $this->options['option_a'],
'@option_b' => $this->options['option_b'],
));
}
}
@sheldonkreger

This comment has been minimized.

Copy link

@sheldonkreger sheldonkreger commented Jul 1, 2014

Great example! Thanks 👍

@xPrzybyLx

This comment has been minimized.

Copy link

@xPrzybyLx xPrzybyLx commented Aug 4, 2014

Do You have some example for three-in-one (custom field handler with custom filter handler with custom sort handler) ?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Aug 12, 2014

+1

@scottalan

This comment has been minimized.

Copy link

@scottalan scottalan commented Aug 27, 2014

👍 Been a while since I'd written a handler. Clean example.

@dimitriadisat

This comment has been minimized.

Copy link

@dimitriadisat dimitriadisat commented Aug 29, 2014

Simple but substantial. +1

@augbog

This comment has been minimized.

Copy link

@augbog augbog commented Sep 3, 2014

Definitely makes things a lot more clearer 👍

For people who are also looking for more examples/explanations of view_handlers, I found some articles. Some of them can be very confusing but I think along with this gist, it makes everything a lot more comprehensible.

A Views handler, the easy way
Creating Views 3 Programmatically

@dresh

This comment has been minimized.

Copy link

@dresh dresh commented Sep 23, 2014

:)

@lmeurs

This comment has been minimized.

Copy link

@lmeurs lmeurs commented Jan 13, 2015

Great write up, many thanks!

@robme

This comment has been minimized.

Copy link

@robme robme commented Jan 23, 2015

This is really helpful. Thank you.

One thing: I think you should use hook_views_data_alter() instead of hook_views_data(), and modify the existing $data['node].

@aaronbauman

This comment has been minimized.

Copy link

@aaronbauman aaronbauman commented Apr 3, 2015

You should not use $values->field_data_field_something_field_something_value as the alias can change depending on base table or relationships.

Instead you should use the field_alias property:
$alias = $this->field_alias;
if (isset($values->{$alias})) {
$my_value = $values->{$alias};
}

@Neograph734

This comment has been minimized.

Copy link

@Neograph734 Neograph734 commented Aug 7, 2015

@aaronbauman, $field_value = $this->get_value($values); would be even better for getting the value of the field.

@tkorakas

This comment has been minimized.

Copy link

@tkorakas tkorakas commented Aug 20, 2015

Awesome!

@priyachat

This comment has been minimized.

Copy link

@priyachat priyachat commented Aug 26, 2015

Hello,
Thanks for this article. My difficulty is I need to create a "My Account" page with views not by profile2.
I have a totally custom registration form with a custom table. So how can I add my all 7-8 custom fields into view to create "My Account" page. Please suggest me.

Thanks,
Priya Roy

@njt1982

This comment has been minimized.

Copy link

@njt1982 njt1982 commented Sep 3, 2015

If you want a simple "render only" field (maybe it calcualtes output based on existing values)... Then you need to implement two methods in the class.

  1. override the query() method and simply do nothing in it.
  2. overrider render() and return the string you need to display, based on the other field values.
@juampynr

This comment has been minimized.

Copy link
Owner Author

@juampynr juampynr commented Mar 20, 2018

Thanks @aaronbauman! Added an alias to the renderer. Then I looked at @Neograph734's comment and decided to use $this->get_value().

@juampynr

This comment has been minimized.

Copy link
Owner Author

@juampynr juampynr commented Mar 30, 2018

Good point @robme. Updated to use hook_views_data_alter().

@juampynr

This comment has been minimized.

Copy link
Owner Author

@juampynr juampynr commented Mar 30, 2018

Added https://gist.github.com/juampynr/27df316e7dab89f02a6d18bbff252bc2 to show a sample Views filter handler in Drupal 7.

@isholgueras

This comment has been minimized.

Copy link

@isholgueras isholgueras commented Apr 3, 2019

Thanks for this example. Very clear

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment