How to disable any block or sidebar from module
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.. Thanks, *vacilando* -- Tomáš J. Fülöpp http://www.vacilando.org
Quoting "Tomá? Fülöpp (vacilando.org)" <tomi@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 -- Earnie -- http://r-feed.com/ -- http://for-my-kids.com/ -- http://www.4offer.biz/ -- http://give-me-an-offer.com/
On Fri, Jul 17, 2009 at 2:01 PM, Earnie Boyd <earnie@users.sourceforge.net>wrote:
Quoting "Tomá? Fülöpp (vacilando.org)" <tomi@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].
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 block 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 page.tpl.php <?php if ($right): ?> <?php if (*!custom_visibility_function*()) ?> <div id="sidebar-right" class="sidebar"> <?php print $right ?> </div> <?php endif; ?> <?php endif; ?> The other option is to use preprocess_block(). Perhaps something like this in template.php <?php function phptemplate_preprocess_block(&$variables) { if ($variables['block']->region = 'right') { if (!custom_visibility_function()) { unset($variables['block']; } } } ?> 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. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
What about using a preprocess function in a module to unset the sidebar region variable if the condition is met? Steve Khalid Baheyeldin wrote:
On Fri, Jul 17, 2009 at 2:01 PM, Earnie Boyd <earnie@users.sourceforge.net <mailto:earnie@users.sourceforge.net>> wrote:
Quoting "Tomá? Fülöpp (vacilando.org <http://vacilando.org>)" <tomi@vacilando.org <mailto:tomi@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 block 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 page.tpl.php
<?php if ($right): ?> <?php if (*!custom_visibility_function*()) ?> <div id="sidebar-right" class="sidebar"> <?php print $right ?> </div> <?php endif; ?> <?php endif; ?>
The other option is to use preprocess_block(). Perhaps something like this in template.php
<?php function phptemplate_preprocess_block(&$variables) { if ($variables['block']->region = 'right') { if (!custom_visibility_function()) { unset($variables['block']; } } } ?>
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 <http://2bits.com>, Inc. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
Here is some code that does a similar thing in preprocess page, to allow you to override the entire sidebar with a CCK text field (if non-empty). You could equally just conditionally unset($vars['right']) instead here. function MYTHEME_preprocess_page(&$vars, $hook) { // The Right Sidebar override uses a CCK field which, when populated, replaces the right sidebar instead of the existing // sidebar right content. if (arg(0) == 'node' && is_numeric(arg(1)) && !arg(2) && isset($vars['node']->field_right_sidebar_override[0])) { $trimmed = trim(strip_tags($vars['node']->field_right_sidebar_override[0]['safe'], '<img>')); if (!empty($trimmed)) { $vars['right'] = $vars['node']->field_right_sidebar_override[0]['safe']; } } } - O On Fri, Jul 17, 2009 at 11:56 AM, Steve Edwards <killshot91@comcast.net>wrote:
What about using a preprocess function in a module to unset the sidebar region variable if the condition is met?
Steve
Khalid Baheyeldin wrote:
On Fri, Jul 17, 2009 at 2:01 PM, Earnie Boyd <earnie@users.sourceforge.net
wrote:
Quoting "Tomá? Fülöpp (vacilando.org)" <tomi@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].
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 block 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 page.tpl.php
<?php if ($right): ?> <?php if (*!custom_visibility_function*()) ?> <div id="sidebar-right" class="sidebar"> <?php print $right ?> </div> <?php endif; ?> <?php endif; ?>
The other option is to use preprocess_block(). Perhaps something like this in template.php
<?php function phptemplate_preprocess_block(&$variables) { if ($variables['block']->region = 'right') { if (!custom_visibility_function()) { unset($variables['block']; } } } ?>
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. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
Owen Your solution takes the cake. Still in the theme though, but very minimal: function MYTHEME_preprocess_page(&$vars, $hook) { if (MYTHEME_custom_condition()) { unset($vars['right']); } } -- Khalid M. Baheyeldin 2bits.com, Inc. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
Might try this module I have never used it but it looks promising http://drupal.org/project/region_visibility On Fri, Jul 17, 2009 at 2:56 PM, Steve Edwards <killshot91@comcast.net>wrote:
What about using a preprocess function in a module to unset the sidebar region variable if the condition is met?
Steve
Khalid Baheyeldin wrote:
On Fri, Jul 17, 2009 at 2:01 PM, Earnie Boyd <earnie@users.sourceforge.net
wrote:
Quoting "Tomá? Fülöpp (vacilando.org)" <tomi@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].
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 block 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 page.tpl.php
<?php if ($right): ?> <?php if (*!custom_visibility_function*()) ?> <div id="sidebar-right" class="sidebar"> <?php print $right ?> </div> <?php endif; ?> <?php endif; ?>
The other option is to use preprocess_block(). Perhaps something like this in template.php
<?php function phptemplate_preprocess_block(&$variables) { if ($variables['block']->region = 'right') { if (!custom_visibility_function()) { unset($variables['block']; } } } ?>
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. http://2bits.com Drupal optimization, development, customization and consulting. Simplicity is prerequisite for reliability. -- Edsger W.Dijkstra Simplicity is the ultimate sophistication. -- Leonardo da Vinci
-- Jim Taylor Rooty Hollow LLC, Owner jim@rootyhollow.com www.rootyhollow.com (614) 432-8609 Twitter: jalama Linkedin: http://www.linkedin.com/in/rootyhollow
participants (6)
-
Earnie Boyd -
Jim Taylor -
Khalid Baheyeldin -
Owen Barton -
Steve Edwards -
Tomáš Fülöpp (vacilando.org)