Custom Views Field Handler - Drupal 7
Drupal 7 230525
Drupal 7 The information below is most suited to Drupal 7. It may or not be applicable to later versions of Drupal
The information is still good, it just needs to be updated for Drupal 8 and beyond: https://api.drupal.org/api/drupal/core%21modules%21views%21src%21Plugin%21views%21field%21FieldPluginBase.php/group/views_field_handlers/8.9.x
Rather than using Views PHP to insert logic in a views field, a highly advised alternative is to write a custom field handler. Â The following code provides the skeleton for a basic Views field handler.Â
example.module
/**
* Implements hook_views_api
*/
function example_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'example'),
);
}
example.views.inc
/**
* @file
* Views definitions for 'example'
*/
/**
* Implementation of hook_views_handlers() to register all of the basic handlers
* views uses.
*/
function example_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'example'),
),
'handlers' => array(
// The name of my handler
'example_handler_custom_field' => array(
// The name of the handler we are extending.
'parent' => 'views_handler_field',
),
),
);
}
/**
* Implements hook_views_data().
*/
function example_views_data() {
$data = array();
// Add Example group
$data['example']['table']['group'] = t('Example');
$data['example']['table']['join'] = array(
// #global is a special flag which let's a table appear all the time.
'#global' => array(),
);
// Add custom field
$data['example']['custom_field'] = array(
'title' => t('Custom Field'),
'help' => t('Provides a custom field.'),
'field' => array(
'handler' => 'example_handler_custom_field',
),
);
return $data;
}
example.info
name = Example Module
description = This module provides the skeleton for a custom Views field handler
core = 7.x
version = 7.x-1.0
dependencies[] = views
files[] = views/example_handler_custom_field.inc
views/example_handler_custom_field.inc
Related articles
Review History
Who | When | Status |
---|---|---|
 |  |  |
Bob | 20230525 | Drupal 7 with suggestions for how to update. |