2

I would like to display a block only on the [root]/dashboard page, and I would like to do this from my custom module defining the block, not from the Drupal admin interface. How can I do this? Thanks you!

Also, where would this condition be better to be written? In hook_block_view() within the module or in the block template file?

2

The code would go in hook_block_info(). You can use the pages property to define the list of pages the block should be shown on. e.g.

$blocks['recent'] = array(
  'info' => t('Recent content'),
  'pages' => 'dashboard',
  'visibility' => BLOCK_VISIBILITY_LISTED
);
  • Incorrect info about visibility. You have to set it to BLOCK_VISIBILITY_LISTED. – kalabro Jul 5 '12 at 10:13
  • please, don't disappoint me! – kalabro Jul 5 '12 at 10:21
  • Sorry I'm struggling to understand what it is you're saying...in what way have I 'disappointed' you?! – Clive Jul 5 '12 at 10:22
  • “visibility: (optional) Initial value for the visibility flag, which tells how to interpret the 'pages' value.” – kalabro Jul 5 '12 at 10:24
  • Ahhhh sorry I didn't understand what you meant there for a minute :) you're right, the visibility would have defaulted to the wrong option for this, I've updated the answer to reflect that. Thanks for the heads-up – Clive Jul 5 '12 at 10:28
0

In hook_block you can assign your block to any page.

 $block = array();
    if ($op == 'list') {
$block[1] = array(
            'info' => 'block info',
            'weight' => -1,
            'status' => 1,
            'region' => 'content_bottom',
            'visibility' => 1,
            'pages' => "test/*" . "\n" . "test2/*",
        );
    }

Where 1 is the delta of the block.

  • That would be for Drupal 6, I think we can infer from the OP's use of hook_block_view() that he's using Drupal 7 – Clive Jul 5 '12 at 10:45

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.