How can I programmatically pull a field from a custom drupal table into drupal 7 views which is joined by a id?

Joby Joseph Source

In regards to drupal 7 - views 3.0 API I have configured a view (from the UI) to pull data from a custom content type and one of the fields it displays ids. I would like to create a join (programmatically i hope) to a custom table of mine and display text that maps to the ids in the view. The problem I have is how do I find out which table and field do I join it to? In my content type I created the field_game field. My custom table has gameid as the primary key.

Maybe something like this?

$data['MYCUSTOMTABLE']['table']['join'] = array(
// Index this array by the table name to which this table refers.
// 'left_field' is the primary key in the referenced table.
// 'field' is the foreign key in this table.
'node' => array(
  'left_table' => '??????'
  'left_field' => 'field_game', 
  'field' => 'gameid',

I've search high and low but nothing really comes close. Any help is appreciated.



answered 6 years ago TDBishop #1

You're close, but check this out: Views has documentation on how to do this in the module code. Check out views/docs/views.api.php and read up! is the source code there, filled with helpful comments (View Source at the bottom).

Basically you'll need to implement hook_views_data_alter to tell Views about your table. This will allow you to join. To display the joined field you'll need to set the correct handler in the array passed to hook_views_data_alter. Look at around line 343 in views.api.php

// This table references the {node} table. The declaration below creates an
// 'implicit' relationship to the node table, so that when 'node' is the base
// table, the fields are automatically available.
$data['example_table']['table']['join'] = array(
  // Index this array by the table name to which this table refers.
  // 'left_field' is the primary key in the referenced table.
  // 'field' is the foreign key in this table.
  'node' => array(
    'left_field' => 'nid',
    'field' => 'nid',

// Next, describe each of the individual fields in this table to Views. This
// is done by describing $data['example_table']['FIELD_NAME']. This part of
// the array may then have further entries:
//   - title: The label for the table field, as presented in Views.
//   - help: The description text for the table field.
//   - relation: A description of any relation handler for the table field.
//   - field: A description of any field handler for the table field.
//   - sort: A description of any sort handler for the table field.
//   - filter: A description of any filter handler for the table field.
//   - argument: A description of any argument handler for the table field.
//   - area: A description of any handler for adding content to header,
//     footer or as no result behaviour.
// The handler descriptions are described with examples below.

// Node ID table field.
$data['example_table']['nid'] = array(
  'title' => t('Example content'),
  'help' => t('Some example content that references a node.'),
  // The nid is a foreign key to the {node} table. This allows us to (easily)
  // add a relationship handler for this table field, making all the table
  // fields for the related node available.
  'relationship' => array(
    'base' => 'node', // The name of the table to join with
    'field' => 'nid', // The name of the field to join with
    'handler' => 'views_handler_relationship',
    'label' => t('Example node'),

answered 6 years ago Willem de Jong #2

I found this post when looking for a similar problem. But I cracked your problem already.

I put this in hook_views_query_alter($view, $query):

if ($view->name == "kompasses") {

$ga_join = new views_join();
$ga_join->table = 'field_data_group_audience';
$ga_join->field = 'entity_id';
$ga_join->left_table = 'node';
$ga_join->left_field = 'nid';
$ga_join->type = 'inner';

$query->add_relationship('audience_node', $ga_join, 'node', null);

Here the table to join is: field_data_group_audience, and the base table (comes from a existing view named "kompasses"). audience_node will be the table alias for the join.

for more info see:

comments powered by Disqus