By enabling the core PHP Filter module, I can use PHP code to set the visibility of a block in the block configure page.

But I don't want to enable the PHP filter module for security measure. So is there a way to do it in custom module? I can't find a hook doing so. And a close hook hook_block_view_alter can only change the block content.

2 Answers 2


Yes. Setting the blocks content to false will hide it from the display and will hide the region if there are no other blocks present. I'd imagine you would need a setup similar to the following:

$block['content'] = _my_module_custom_block();


function _my_module_custom_block(){
        return false;
  • 2
    wouldn't it still show a block with empty content?
    – gilzero
    Commented Jan 5, 2012 at 15:18
  • 1
    works, great. thx.
    – gilzero
    Commented Jan 5, 2012 at 15:24
  • @gilzero , no it would not.
    – W.M.
    Commented Aug 20, 2017 at 17:04

The Rules block visibility module is another alternative available ... which doesn't require the PHP filter to be enabled. 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.

Moreover, as per the "if you can do it with Rules, you can use it to control block visibility" above, you have the power of Rules available to implement all sorts of custom logic (cfr. the "programmatically / custom module" part of your question).

For an illustration of how to use this module, refer to my answer to "How to stop a Views block from displaying to admins, such as user/1?".

Your Answer

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

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