2

I need to show blocks on different url's.

Block1 
Should be displayed on:
branches/taxonomytermlevel1
But not on
branches/taxonomytermlevel1/taxonomytermlevel2

Block2 
Should be displayed on
branches/taxonomytermlevel1/taxonomytermlevel2
But not on
branches/taxonomytermlevel1

The terms /taxonomytermlevel1/taxonomytermlevel2 are variables (elements in some taxonomy list)

What is the most easiest way to do this? I think a php script in the block display visibility setting would be the trick and so I tried some code there but I did not succeed.

Some code I have now is e.g.:

<?php
if (empty(arg(3))) {
  return TRUE;
}else {
  return FALSE;
}
?>

Any suggestions?

0

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);
0

There is another way to answer your question, without PHP in the block settings: you can use the Rules block visibility module for this. Here is a quote from its project page:

The Rules block visibility module allows Rules components to be used to control block visibility. This provides Drupal administrators and developers extreme flexibility in controlling when blocks should be displayed on their websites, in addition to the default visibility options provided by Drupal.

The general idea is that if you can do it with Rules, you can use it to control block visibility, so the possibilities are limitless.

Need to show a block only for users registered more than a month ago?

Perhaps you have a block that must be shown only between 8am-5pm on weekdays?

What about displaying or hiding a block based on current weather conditions?

All of this can be done by using Rules block visibility.

With that, and as per the "if you can do it with Rules, you can use it to control block visibility" above, you've reduced your question to making Rules "check the path of the current page" (so that the appropriate menu block is shown on the appropriate pages).

For an illustration of how to use this module, refer to my answer to "How to show blocks by url, when nodes have multiple paths?". It includes some Rules Components (in Rules export format), which you should replace by the your variations to make it match your specific question.

So with 2 (similar) rules, 1 for each of both blocks, you should be able to get this to work.

To actually "determine the path structure in rules" (as in your extra comment below this answer), you might want to use a Rules Condition using a regular expression. Similar to the Rules Condition in the rule I included in my answer to "How to automatically skip intermediate redirects?".

Using regular expressions opens up many more situations (on top of the Rules block visibility module) where this solution can work. If you're not familiar with Regular Expressions (not obvious ...), then maybe you also want to look at my answer to "How to suppress a core Drupal warning or error message set with drupal_set_message for site users?".

Bonus:

This approach does NOT require the PHP filter to be enabled either (which you should try to avoid whenever possible) ...

  • thxs I will look into this. Sounds good ! – Justme Aug 15 '16 at 8:44
  • Did not knew about this rules extension and I like it. Bssic stuff is running. So how to determine the path structure in rules? How to check in rules if the url contains 1 or 2 term elements? – Justme Aug 15 '16 at 12:20
  • Check my updated answer. If that doesn't help, I suggest you create a followup question (maybe referring to this one if needed), ok? Also, as per your "basic stuff is running": are you familiar with this? – Pierre.Vriens Aug 15 '16 at 12:42
  • Wauw what a nice combination to use it with regex. ! – Justme Aug 15 '16 at 13:32
  • 1
    Hi pierre, just send you a new email :-( Did try this solution yet. It is on my list, if it is working I will accept it. Be patient :-) – Justme Aug 26 '16 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.