I am using hook_query_alter to make changes in get taxonomy terms query.
function MYMODULE_query_alter(QueryAlterableInterface $query) {
global $user;
if($user->uid > 1) {
if ($query->hasTag('term_access') && strstr(current_path(), 'admin/structure/taxonomy')) {
$permission = $query->leftJoin('term_permissions_user', 'tpu', "t.tid = tpu.tid");
$ored = db_or();
$ored->condition('uid', NULL, 'IS');
$ored->condition('uid', $user->uid, '=');
$anded = db_and();
$anded->condition($ored);
$query->condition($anded);
}
}
}
I want to have above query clause on the page which lists taxonomy terms only but this hook is adding the clause on all pages. I mean this hook is called on any page load of my drupal site.
Other issue is that the alias for table taxonomy_term_data is base
on same pages and t
on other pages and if i join using alias t
it gives me error on most of the pages and vice versa.
I am checking the table alias to ensure the use of table alias used in my query and have changed the code as
function MYMODULE_query_alter(QueryAlterableInterface $query) {
global $user;
if($user->uid > 1) {
if ($query->hasTag('term_access') && strstr(current_path(), 'admin/structure/taxonomy')) {
$tables = $query->getTables();
if(isset($tables['t']) && isset($tables['t']['alias']) && $tables['t']['alias'] == 't' && $tables['t']['table'] == 'taxonomy_term_data') {
$permission = $query->leftJoin('term_permissions_user', 'tpu', "t.tid = tpu.tid");
$ored = db_or();
$ored->condition('uid', NULL, 'IS');
$ored->condition('uid', $user->uid, '=');
$anded = db_and();
$anded->condition($ored);
$query->condition($anded);
}
}
}
}
But I am not sure if I have used a correct check, as of now my aim of adding query clause is fulfilled. Just to ensure this will work for long and will not break in any case. Please help me to have this hook call on the page where I need this and use the exact table alias.
Thanks in advance for your help.