How to Add Custom table in Views Query Alter tutorial

Issue: How to Add Custom Table in View Query Alter.

Solution:
We can add our custom table in view query with the help of hook_views_query_alter .

I have table name Custom Table name "custom_node" and I want add this table in my views Query to make the Condition result for View.

Code Example:
function hook_views_query_alter(&$view, &$query){
if($view->name == "view_machine_name") {
$join = new views_join();
$join->table = ‘custom_node’;
$join->field = ‘custom_id’;
$join->left_table = ‘node’;
$join->left_field = ‘nid’;
$join->type = ‘inner’;

$query->add_relationship(‘custom_node’, $join, ‘node’);

$query->where[1]['conditions'][] = array(
‘field’ => ‘custom_node.custom_nid’,
‘value’ => array(‘7’), // Make your condition with the help of Where Clause
‘operator’ => ‘in’
);

}

}

Advertisements

One thought on “How to Add Custom table in Views Query Alter tutorial

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

WordPress.com.

Up ↑

%d bloggers like this: