10

Consider a module deserts:

  • page1:- deserts/*
  • page2:- deserts/*/*
  • page3:- deserts/*/*/*

A dynamic content ...

I have three blocks, namely a,b and c:

  • Block a should be displayed only in page1
  • Block b should be displayed only in page2
  • Block a should be displayed only in page3

How should do that restriction?

7
  • Drupal 6 or 7? 6 makes it easier to define PHP code for block visibility.
    – Mołot
    Aug 30, 2013 at 14:20
  • It's drupal7 molot
    – Ajmal
    Aug 30, 2013 at 14:21
  • 2
    @Ajmal It would be useful if you could comment on the given answers and explain why they haven't helped. People will (naturally) assume that the high-voted answer is the correct one, and not go on to provide an alternative, if you haven't explained why that answer doesn't work for you.
    – Clive
    Sep 6, 2013 at 13:36
  • I have the page desert/*/*/ desert/*/edit my block should display for all the desert/*/* not for desert/*/edit In such case it doesn't works for me..
    – Ajmal
    Sep 6, 2013 at 13:50
  • 1
    Maybe it's just me, but this seems confusing. * means wildcard. So asking for a solution where the block appears for desert/*/* implies that it should show up for desert/*/edit or, for that matter, desert/*/whatever.
    – cdmo
    Sep 6, 2013 at 14:13

3 Answers 3

18
+50

Use the context module. Though I'm not generally a fan of this module, this is one of the cases where it comes in handy.

Add a condition for a path. So if I want a block on page 1, but not page 2 or 3, I would write the paths like so, one for each block:

Case 1 - Block A

deserts/*
~deserts/*/*
~deserts/*/*/*

Case 2 - Block B

~deserts/*
deserts/*/*
~deserts/*/*/*

Case 3- Block C

~deserts/*
~deserts/*/*
deserts/*/*/*

The ~ means to exclude in this case. After that, add the block to the region you would like for it to show up. You can also add back in specific paths that might otherwise be excluded, and vice versa.

2
  • It holds good for this case. But we want to fix this for all the possible scenarios, that is the original solution, there is an case where it doesn't holds good. i have explained that case above. so please kindly refer that
    – Ajmal
    Sep 7, 2013 at 7:12
  • I updated my answer, so hopefully this will explain it better. You would make one rule for each block. Sep 9, 2013 at 14:58
4

You can use the "Pages on which this PHP code returns TRUE (expert only)" visibility option. But putting PHP code in the database is, IMHO, a bad practice. Instead, consider using something like the Extended block visibility module or implementation of hook_block_list_alter() such as

function MODULE_block_list_alter(&$blocks) {
  global $theme_key;
  foreach ($blocks as $key => $block) {
    if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {
      // This block was added by a contrib module, leave it in the list.
      continue;
    }
    switch ("{$block->module}_{$block->delta}") {
      case "moduleA_deltaA":
        if (arg(0) != 'deserts' || !arg(1) || arg(2)) {
          unset($blocks[$key]);
        }
        break;
      case "moduleB_deltaB":
        if (arg(0) != 'deserts' || !arg(1) || !arg(2) || arg(3)) {
          unset($blocks[$key]);
        }
        break;
      case "moduleC_deltaC":
        if (arg(0) != 'deserts' || !arg(1) || !arg(2) || !arg(3)) {
          unset($blocks[$key]);
        }
        break;
    }
  }
}

Note: There is probably a smarter way to implement your visibility rule.

0

You should be able to get this to work using the Rules block visibility module. 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 intercept the various URLs corresponding your page1, page2 and page3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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