Add custom table to Views in Drupal 6

While trying to fix an issue with google geocoding, I wanted to use a separate table for a list of “annonces” (content type) and then wanted to display the content of that table with views module.

By default views don’t display a custom table content but it provides a hook to do this – hook_views_data().

I have included this hook in mymodulename.views.inc file, which I created in a subdirectory named ‘includes’ of my module directory.

Here is the code in mymodulename.views.inc file :

function mymodulename_views_data() {
  // The 'group' index will be used as a prefix in the UI for any of this
  // table's fields, sort criteria, etc.
  $data['example_table_name']['table']['group'] = t('Import annonces');
  // Define this as a base table. 
  $data['example_table_name']['table']['base'] = array(
    'field' => 'annonce_id',
    'title' => t('example_table_name table'),
    'help' => t("Table contains imported annonces"),
    'weight' => -10,
  );
  // This table creates an 'implicit' relationship to the node table, so that when 'Node'
  // is the base table, the fields are automatically available 
  $data['example_table_name']['table']['join'] = array(
    'node' => array(
      'annonce_id' => 'nid',
      'field' => 'nid',
     ),
  );
  // Now, explain all the fields in this table. For
  // each field, you can define what field, sort, argument, and/or filter
  // handlers it supports. This will determine where in the Views interface you
  // may use the field.
  // Node ID field.
  $data['example_table_name']['nid'] = array(
    'title' => t('Node content'),
    'relationship' => array(
      'base' => 'node',
      'field' => 'nid',
      'handler' => 'views_handler_relationship',
      'label' => t('Node id in this table that references a node.'),
    ),
  );
  // Explain numeric fields in the table
  $data['example_table_name']['annonce_id'] = array(
    'title' => t('Id annonce'),
    'help' => t('annonce id'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  $data['example_table_name']['filename'] = array(
    'title' => t('File name'),
    'help' => t('File imported'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  $data['example_table_name']['files_count'] = array(
    'title' => t('Files count'),
    'help' => t('Count of imported files'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
      'click sortable' => TRUE,
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
  );
  return $data;
}

and then added this code to mymodule.module to get views to know this new table:

/**
 * Implementation of hook_views_api().
 * 
 */
function mymodulename_views_api() {
	return array(
			'api'  => 2.0,
			'path' => drupal_get_path('module', 'mymodulename') . '/includes',
	);
}

Hope this will be helpful!

Advertisements