Breaking

Followers

Wednesday, 8 February 2017

How to Add Custom table in Views Query Alter

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.

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'
);
        
    } 
    
 

}

No comments:

Post a Comment