I'm new to both Drupal and php, but have nearly finished my first site - very happy! I have searched the forums and handbooks and found lots of similar things, but still haven't managed to figure out the answer. It seems to me that it should be fairly simple and I am just missing something!

I wish to use the (4.7) functionality to use php to set block visibility settings so that a custom block will display only on any pages that do not have any other block displayed in the left sidebar.

I know I can hard code this by setting the block to display manually on the relevant nodes, but would like to know how to do it in a more smooth way.

I have played around with various function calls and $left_sidebar, $blocks[region] etc unsuccessfully.

Does anyone have any ideas?

Many thanks.

Comments

amnon’s picture

An intereseting question.

I know how to do that, but it involves modifying the theme, and it has nothing to do with the visibility settings of your custom block.

First you should creart a function mytheme_regions() to define a new area, "look_here", where you put your custom block(s) in.

function mytheme_regions() {
  return array(
       'left' => t('left sidebar'),
       'right' => t('right sidebar'),
       'content' => t('content'),
       'header' => t('header'),
       'footer' => t('footer'),
       'look_here' => t('Content that would be shown if there\'s nothing at the left sidebar'),
  );
}

Next, define the _phptemplate_variables() function, to load the 'look-here' area into 'sidebar_left' if it's empty:

function _phptemplate_variables($hook, $vars = array())
{
  if ($hook == 'page') {
    if (!$vars['sidebar_left'])
      return array( 'sidebar_left' => theme('blocks', 'look_here') );
    }
  }
  return array();
}

Haven't checked the code, but it should work.

celia_m’s picture

Thanks, Amnon, that's great and it does work! (needed to add a couple of brackets if anyone else uses this)

I still have a problem though, in that the central content for those pages doesn't get shifted sideways, so overwrites the block.

The reason I wanted to set a block for all pages without a sidebar_left block was to maintain the content in the central area. Probably limitation with my template, but the difference in source code is that the sidebar left class is enclosed in a different wrapper for the newly created region.

I don't know whether this is due to the order of function calls on the page - will have a play around.

Thanks again!

amnon’s picture

I still have a problem though, in that the central content for those pages doesn't get shifted sideways, so overwrites the block.

What theme are you using?

Here is an updated _phptemplate_variables(), please use it:

function _phptemplate_variables($hook, $vars = array())
{
  $ret = array();
  if ($hook == 'page') {
    if (!$vars['sidebar_left']) {
      $ret = array(
        'sidebar_left' => theme('blocks', 'look_here'),
        'layout' => 'both',
      );
    }
  }
  return $ret;
}

The change is the addition of the 'layout' variable. phpTemplate sets it to 'right' if there's nothing at the left sidebar, so here I'm overwriting it to 'both', to notify the theme that both bars (reft and right) exist nevertheless.

But few themes actually use the 'layout' variable, so it may have no effect in your case.

I haven't checked the code. If it doesn't work, let me know what theme you're using and I'll try to have a look.

celia_m’s picture

amnon

I can't thank you enough! You have put so much thought and time into it. It has really made my day and is very much appreciated. It is thanks to people like you who will help people like me that makes this community great.

I used the amended _phptemplate_variables() function and it worked perfectly (my theme is based on friendselectric by Steven Wittens). You have also given me insight into the inner workings of Drupal by explaining what you have done and different options, which is great as it helps me to learn.

Hopefully one day I'll be able to repay the favour to some other lost newbie. :-)

Thanks again.

amnon’s picture

The reason I wanted to set a block for all pages without a sidebar_left block was to maintain the content in the central area.

Oh, so that's the reason :-)

Then maybe there's a simpler solution: add an "invisible" block to the left bar.

I haven't checked if the following plan actually works. I'm just giving you an idea.

Drupal assigns every block a unique ID (something like 'block-xxx-#'), so in your CSS file set your custom's block visibility to hidden. It would be a normal block, but the browser just won't show it.

Don't use 'display: none', but 'visibility: hidden', because the former doesn't take up space, whereas the later does. (But please check your CSS reference for the correct name of this attribute -- It's been ages since I worked with CSS.)

amnon’s picture

maybe there's a simpler solution

But why don't you just put a 'width' CSS attribute on your left bar?

If the left bar is not shown when empty, hack the theme to show it nevertheless. E.g., if your page.tpl.php has:

<?php if ($sidebar_left) { ?><td id="sidebar-left">
    <?php print $sidebar_left ?>
</td><?php } ?>

change it to:

<td id="sidebar-left">
    <?php print $sidebar_left ?>
    &nbsp;
</td>

Don't take my advice literally: I don't know exatcly what you have there. I'm just trying to give ideas.