Hi all,

I am trying to display a block based on URL hierarchy depth.

My URL hierarchy is like 1/2/3/4/5/6

I have a block which I want to show only for 1/2/3 and 1/2/3/*
but not for 1/2/3/*/* or 1/2/3/*/*/*.

I tried the following using the option
Show on only the listed pages
1/2/3
1/2/3/*

This does not work. It shows for all 4 types, 1/2/3, 1/2/3/*, 1/2/3/*/* and 1/2/3/*/*/*.

Show on only the listed pages
1/2/3
1/2/3/*/

This also does not work. This shows only for 1/2/3 only.

Can you please help me out?

Thanks,
Mrigank.

Comments

nevets’s picture

I would use the PHP option and the code

return empty(arg(4));
mrigank’s picture

I tried this but the problem is that these are taxonomy pages with Path Auto used to give meaningful URLs.
Using the arg() API returns the value from the URL type taxonomy\term\*

To put the problem in a different way, I want to display the block only if the URL contains a given taxonomy term or its children.
The below code works for a given term id, but does not show the block for the children

$termid = arg(2);
if($termid == 2)
    return TRUE;
else
    return FALSE;

One option is to hard code all children ids or use the API to get children for a tid. I am trying that now.

WillHall’s picture

<?php
if (arg(0) != "" || arg(1) != "" || arg(2) != "" && arg(3) == "") {
  return true;
} else {
  return false;
}
?>

Here is how you can get terms in the mix as well.
http://drupal.org/node/69076

mrigank’s picture

Hi all,

Thanks for the help.

This code snippet finally worked

$match = FALSE;

// put here the vocabulary ID you're interested in
$vid = 1;
$tid = 2;

  if (arg(0) == 'taxonomy' && arg(1) == 'term') {
  $str_tids = arg(2);  
  $terms = taxonomy_get_tree($vid, 2);
  foreach ( $terms as $term ) { 
    $items[] = $term->tid;
  }
  
  $terms = taxonomy_terms_parse_string($str_tids);  
  
  //browse each value for children tid
  foreach ($terms['tids'] as $k => $v) {
    if (in_array($v, $items)) {
      $match = TRUE;
    }
  }
  // Check if parent tid is there in list
  foreach ($terms['tids'] as $k => $v) {
    if ($v == $tid) {
      $match = TRUE;
    }
  }
}
return $match;

Thanks,
Mrigank.

WillHall’s picture

nm - nevets beat me to it.