I currently have a unique situation whereby I need a to use a different term reference field in a view's contextual filters based on a node's content type value. I don't want to have to manually add a display for each possible combination of content types so I have been tying to use hook_views_query_alter() to accomplish what is now seeming impossible to me.
In my .module file I have:
/**
* Implementation of hook_views_api().
*/
function mymodule_views_api() { // your module name into hook_views_api
return array(
'api' => 3,
'path' => drupal_get_path('module', 'mymodule').'/includes',
);
}
Then in the file mymodule/includes/mymodule.views.inc:
/**
* Implements hook_views_query_alter()
*/
function mymodule_views_query_alter(&$view, &$query) {
//this is the name of our View and Display
if ($view->name == 'shop' && $view->current_display == 'listings') {
$query->add_table('field_data_field_my_category');
//then add where clause afterwards etc etc
}
}
This throws the error:
Fatal error: Call to undefined method SelectQuery::add_table()
Which leads me to believe that the Views API is not loading properly, unless I'm completely on the wrong track here.
I've been trying to find the best practices for altering views queries but have just ended up being generally confused and frustrated. Could anyone lead me towards the light please?