Drupal - How to display the blocks to display in certain page in Drupal?

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.


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.

Tags:

7

Blocks