Custom Drupal Views and Fields using hook_views_api() and hook_views_data()

  • strict warning: Non-static method view::load() should not be called statically in /home1/nate01/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_handler_field::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_field.inc on line 1147.
  • strict warning: Declaration of views_handler_argument::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_argument::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_argument.inc on line 917.
  • strict warning: Declaration of views_handler_sort::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_sort::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_sort.inc on line 165.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_handler_filter::query() should be compatible with views_handler::query($group_by = false) in /home1/nate01/public_html/sites/all/modules/views/handlers/views_handler_filter.inc on line 587.
  • strict warning: Declaration of views_plugin_query::options_submit() should be compatible with views_plugin::options_submit($form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_query.inc on line 169.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_row.inc on line 136.
  • strict warning: Non-static method view::load() should not be called statically in /home1/nate01/public_html/sites/all/modules/views/views.module on line 1113.
  • strict warning: Declaration of views_plugin_pager_none::post_execute() should be compatible with views_plugin_pager::post_execute(&$result) in /home1/nate01/public_html/sites/all/modules/views/plugins/views_plugin_pager_none.inc on line 69.

Create a custom module to use a database table for a view.

Replace MODULENAME with the name of your module and TABLENAME with the name of your table.

MODULENAME.info
name = MODULENAME
description = This module shows fields from a db table using views
dependencies[] = views
core = 6.x

MODULENAME.module
<?php
function MODULENAME_views_api() {
	return array(
		'api' => 2,
		'path' => drupal_get_path('module', 'MODULENAME') . '/modules',
	);
}

MODULENAME/modules/MODULENAME.views.inc
<?php
function MODULENAME_views_data() 
{
	$data['TABLENAME']['table']['base'] = array(
		'field' => 'id',
		'title' => t('Email Archive Table'),
		'help' => t('An archive of emails sent by the site.'),
		'weight' => -10,
	);
 
	$data['TABLENAME']['table']['group'] = t('Email Archive');
 
	$data['TABLENAME']['body'] = array(
		'title' => t('Email Body'), 
		'help' => t('The message sent in the email.'), 
		'field' => array(
			'handler' => 'views_handler_field', 
			'click sortable' => TRUE,
		), 
		'sort' => array(
			'handler' => 'views_handler_sort',
		), 
		'filter' => array(
			'handler' => 'views_handler_filter_string',
		), 
		'argument' => array(
			'handler' => 'views_handler_argument_string',
		),
	);
 
	$data['TABLENAME']['subject'] = array(
		'title' => t('Email Subject'), 
		'help' => t('The subject of the email.'), 
		'field' => array(
			'handler' => 'views_handler_field', 
			'click sortable' => TRUE,
		), 
		'sort' => array(
			'handler' => 'views_handler_sort',
		), 
		'filter' => array(
			'handler' => 'views_handler_filter_string',
		), 
		'argument' => array(
			'handler' => 'views_handler_argument_string',
		),
	);
 
	$data['TABLENAME']['date'] = array(
		'title' => t('Timestamp field'), 
		'help' => t('The date/time of the email.'), 
		'field' => array(
			'handler' => 'views_handler_field_date', 
			'click sortable' => TRUE,
		), 
		'sort' => array(
			'handler' => 'views_handler_sort_date',
		), 
		'filter' => array(
			'handler' => 'views_handler_filter_date',
		),
	);
 
	return $data;
}
?>

Other Helpful Links about Views

Describing tables to Views

Views Docs