Custom Field Handlers for Views 3

You, DrupalDrupal views
Back

Here are the basic steps to create custom field handler for views 3.

  1. Define supported version of views API in your .module file.
<?php
/**
* Implementation of hook_views_api().
*/
function video_views_api() {
  return array(
    'api' => 3.0,
    'path' => drupal_get_path('module', 'video') . '/views',
  );
}
?>
  1. Create your table struscture for MODULENAME.views.inc file.
<?php
/**
* @file
* Provide views data and handlers for statistics.module
*/
/**
* @defgroup views_statistics_module statistics.module handlers
*
* Includes the ability to create views of just the statistics table.
* @{
*/

/**
* Implements hook_views_data()
*/
function video_views_data() {
  // Basic table information.
  // ----------------------------------------------------------------
  // video_output table

  $data['video_queue']['table']['group'] = t('Video');

  $data['video_queue']['table']['join'] = array(
    // ...to the node table
    'node' => array(
      'left_field' => 'nid',
      'field' => 'nid',
    ),
  );
?>
  1. Create your custom field file. video_handler_field_video_duration.inc in my case.
<?php
/**
* Field handler to provide simple renderer that turns a URL into a clickable link.
*/
class video_handler_field_video_duration extends views_handler_field {

  function render($values) {
    if (!empty($values->video_queue_duration))
      return gmdate("H:i:s", $values->video_queue_duration);
    else
      return NULL;
  }

}
?>
  1. Let views know about your file.

4.1 Add video_handler_field_video_duration.inc to your module.info as files[] = video_handler_field_video_duration.inc

4.2 Implement hook for views

<?php
/**
* Implementation of hook_views_handlers().
*/
function video_views_handlers() {
  return array(
    'info' => array(
      'path' => drupal_get_path('module', 'video') . '/views',
    ),
    'handlers' => array(
      'video_handler_field_video_duration' => array(
        'parent' => 'views_handler_field',
      ),
    ),
  );
}
?>
© Heshan Wanigasooriya.RSS

🍪 This site does not track you.