[development] How to disable any block or sidebar from module

Khalid Baheyeldin kb at 2bits.com
Fri Jul 17 18:47:13 UTC 2009

On Fri, Jul 17, 2009 at 2:01 PM, Earnie Boyd
<earnie at users.sourceforge.net>wrote:

> Quoting "Tomá? Fülöpp (vacilando.org)" <tomi at vacilando.org>:
>  Hi,
>> I cannot find a generic way how to disable a sidebar programmatically.
>> In my module, if some conditions are met, I would like to make sure that
>> the
>> whole sidebar (or any blocks in it) are not rendered at all, regardless of
>> the theme.
>> Could somebody advise, please? Sorry if it's obvious..
> I think you want to use hook_block()[1].
> [1] http://api.drupal.org/api/function/hook_block

He is talking about the whole sidebar, hence a region, not a block.
hook_block() will not help here.

In the past we have done it by setting the visibility PHP code for each
in a given region, and that would make the entire sidebar invisible if the
conditions are met. It is a pain if you have more than a few blocks in that
region (right side bar for example).

You said you don't want it in the theme, but it would be a simple change in

      <?php if ($right): ?>
        <?php if (*!custom_visibility_function*()) ?>
          <div id="sidebar-right" class="sidebar">
            <?php print $right ?>
        <?php endif; ?>
      <?php endif; ?>

The other option is to use preprocess_block(). Perhaps something like this

function phptemplate_preprocess_block(&$variables) {
  if ($variables['block']->region = 'right') {
    if (!custom_visibility_function()) {

Still in the theme though.

> --
> Earnie
> -- http://r-feed.com/           -- http://for-my-kids.com/
> -- http://www.4offer.biz/       -- http://give-me-an-offer.com/

Khalid M. Baheyeldin
2bits.com, Inc.
Drupal optimization, development, customization and consulting.
Simplicity is prerequisite for reliability. --  Edsger W.Dijkstra
Simplicity is the ultimate sophistication. --   Leonardo da Vinci
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.drupal.org/pipermail/development/attachments/20090717/33fbb45d/attachment.htm>

More information about the development mailing list