Keeping Some Regions around for Panels that use the "disable drupal blocks" setting

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
joshk's picture

I (heart) panels, and my clients are crazy for them. It's simply the most intuitive way to handle content layout.

All of the recent projects I've worked on have been designed around making panels fit in easily. One of the tricks I've discovered is a simple technique to use in template.php for keeping some block regions around (e.g. regions in the header/footer area, useful for nav, banner ads, etc) even when using the "Disable Drupal blocks/regions" checkbox from the Advanced pane.

Here's the code:

<?php
   
// Retain some block regions even if cleared by panels...
   
$save_regions = array('header', 'bottom_banner', 'footerblocks');
    foreach(
$save_regions as $id) {
      if (
$vars[$id] == '') {
       
$vars[$id] = theme('blocks', $id);
      }
    }
   
// special handling for $footer_message
   
if ($vars['regions']['footer'] == '') {
     
$vars['footer_message'] .= "\n". theme('blocks', 'footer');
    }
?>

Now, a little explanation and some questions.

First of all, the use-case, e.g. why this is even necessary. As I said, clients love panels. Because it allows an easy visual layout, it makes sense to them in ways that nodes almost never do. Still, I haven't yet been able to build a site completely out of panels. There are always custom menu callbacks, and overriding node/% and user/% (while great emerging techniques) can sometimes be problematic or restrictive. The end result is that we always need to be able to "fall back" out of panels, and still have some sane sidebar structure.

That means designing a theme with the usual drupal sidebar, then designing custom panels that mimick the exact same markup. Not really all that hard. I recommend it for everyone.

The trouble comes when using that "Disable Drupal blocks/regions" checkbox from the Advanced pane. If you're like me and you have several other active regions that are "outside" the main content area, and you want to continue managing those via the normal block interface because it makes it easy to administer (as opposed to custom theme settings, hard-coding, etc), you have a dilemma. Any time a panel pane or node "takes over," it clears all those other regions too.

There are other ways of handling this. One is to not use the checkbox and add some other detection for whether or not the sidebar should be present. I haven't yet found a reliable method there, so I go the reverse route: in a page_preprocess (or _phptemplate_variables() in 5.x) function, I check and make sure that these regions are populated, using the very same methods that drupal normally would if the absence of the $show_blocks variable hadn't prevented them from being rendered.

So that's the story. I'm curious if anyone can poke holes in this theory, or has other techniques to share for more deeply integrating panels into your sites.

Happy Drupaling!

Comments

one checkbox?

jeff h's picture

To be honest, I was initially quite surprised that there was just one checkbox to turn regions on or off. I'm almost certain that in some previous version of Panels this checkbox turned on/off just the left and right column regions (assuming they existed in the theme). This was ok, so long as those were the only regions related to the panels area (ie $content).

I think it would be much more flexible if we could choose to activate/de-activate any of the regions in the current theme — I guess that's a bit difficult to implement given the possibility of multiple themes in one site though. Not sure of a solution that copes with that, other than at the theme-level itself, as you've done (which looks neat).

We recently built a site which passes everything through panels, with some clever code in the template.php which dynamically loads a specific panel on non-panel pages, but this can lead to all kinds of scariness :o

Jeff

Don't think so

Michelle's picture

I can't swear to it, but I'm fairly sure the method panels uses to get rid of the regions is an all or nothing thing. It's not actually panels, technically, but some theme function/variable I've forgotten the name of. I remember merlin talking once about some themes that didn't honor the function/variable and the trick didn't work with them because of it.

Michelle


See my Drupal articles and tutorials or come check out the Coulee Region

It's theme('page') that

merlinofchaos's picture

It's theme('page') that controls it, and a patch to Drupal at some point 'fixed' the bug that was making only sidebar content not show up when regions were supposed to be off.

Made my day

laken's picture

Thanks Josh, just what I needed. Auspicious beginning for my day!

An idea

mlconnor's picture

I thought it would be nice to create a Panels layout that actually mimics the theme. The idea being that you could have a pane called 'first-sidebar' and 'second-sidebar' so that you could control the content going into the regions using Panels. This wouldn't be appropriate for all cases but would be a nice mixture of templates and panels. Right now, it's kind of all or nothing. I don't want to have to create CSS for my region sidebar and my Panels sidebar and then keep them in sync.

I saw that the Zen Panels project was trying to do something like this but it doesn't look complete, or even under continued development.

I know this is an old thread

gmclelland's picture

I know this is an old thread but here is how I do it:

// Remove sidebars if disabled e.g., for Panels, copied from Fusion theme

  if (!$vars['show_blocks']) {

    $vars['left'] = '';

    $vars['right'] = '';

  }