Arguments are not URL parts. In your case arg(3) will always be empty, because the internal Drupal path for taxonomy terms will be taxonomy/term/[tid]
, for any term level.
Solution #1. Based on taxonomy terms.
Your PHP code should get all the parents and calculate the amount of them.
For Block #1
if (arg(0) == 'taxonomy' && arg(1) == 'term' && !empty(arg(2))) {
$tid = intval(arg(2));
$parents = taxonomy_get_parents_all($tid);
return (count($parents) == 1);
}
For Block #2
if (arg(0) == 'taxonomy' && arg(1) == 'term' && !empty(arg(2))) {
$tid = intval(arg(2));
$parents = taxonomy_get_parents_all($tid);
return (count($parents) == 2);
}
The reason why we use count($parents) as 1 and 2 is that the function taxonomy_get_parents_all()
returns all the parents and the term itself. So for the A-B-C-D hierarchy you will be given:
array(
0 => D (term object),
1 => C (term object),
2 => B (term object),
3 => A (term object)
);
Also this would only work if your terms have only 1 parent. In working with a taxonomy in which a term can have multiple parents, you will have to gather all the instances where the count is 1 and return an array of those terms.
Solution #2. Based on the URLs.
Only if all your terms have aliases and all the aliases follow the hierarchy level. I.e. for your example both URLS are correct branches/taxonomytermlevel
and branches/taxonomytermlevel1/taxonomytermlevel2
.
For Block #1
$path = drupal_lookup_path('alias', current_path());
$exploded_path = explode($path);
$path_count = count($exploded_path);
return (count($path_count) == 2);
For Block #2
$path = drupal_lookup_path('alias', current_path());
$exploded_path = explode($path);
$path_count = count($exploded_path);
return (count($path_count) == 2);