3

I'm looking for a way to force an empty region to display. I notice Omega does this via a theme-settings checkbox, but I'm unable to track down how it's done.

Regardless of removing the if ($page['regionname']): around print render, the region still does not display unless it's populated with block content. Perhaps injecting content into the $content var somehow?

Example:

In a custom theme, the header variables such as logo, sitename and secondary links are placed in a region template called region--branding.tpl.php.. (the variables have to be passed from preprocess_page to preprocess_region so this can work and a template suggestion needs to be made). Once this region is printed in page.tpl.php, it must always be rendered even if there is no block content in that area.

1
  • I was able to solve this with HOOK_page_alter
    – krisbulman
    Commented Jun 10, 2012 at 12:28

4 Answers 4

3

Omega does it this way:

in omega.info

settings[alpha_region_xxx_force] = ''

in `omega/alpha/includes/theme-settings-structure.inc

$form['alpha_settings']['structure'][$section][$zone]['regions'][$region]['alpha_region_' . $region . '_force'] = array(
  '#type' => 'checkbox',
  '#title' => t('Force this region to be rendered'),
  '#description' => t('Enabling this will always render this region, even if it is empty.'),
  '#default_value' => $item['force'],
);

Call alpha_alpha_page_structure_alter() in alpha_page_alter()

#omega/alpha/template.php
foreach ($theme->regions as $region => $item) {
    if (($item['force'] || !empty($vars[$region]))) {
      $temporary[$item['section']][$item['zone']][$region] = !empty($vars[$region]) ? $vars[$region] : array();
...

this way your code should be:

function THEMENAME_page_alter(&$page) {
    $regions = system_region_list($GLOBALS['theme'], REGIONS_ALL);
    foreach ($regions as $region => $name) {
        $page[$region] = !empty($page[$region]) ? $page[$region] : array();
    }
}
1

Here's an alternative way, quite hacky but it works - essentially create a fake block in each empty region in hook_preprocess_page()

function MYTHEME_preprocess_page(&$vars){
  global $theme;
  $regions = array_keys(system_region_list($theme));
  foreach($regions as $region){
    if (empty($vars['page'][$region])){
      $vars['page'][$region] = array(
        'not_really_a_block' => array(
          '#markup' => '',
        ),
        '#theme_wrappers' => array('region'),
        '#region' => $region,
      );
    }
  }
}
1

The answers given here were not satisfactory, they either overwrote content that might have been added to the block (krisbulman), or they were not sufficient to really force the block to display (Mohammad). So here is the solution i finally stuck with:

Problem

Force the display of one specific region with the name REGIONNAME in the theme THEMENAME

Solution

function THEMENAME_page_alter(&$page) {
    if ( !isset($page["REGIONNAME"]) || empty($page["REGIONNAME"])) {
        $page["REGIONNAME"] = array(
            '#region' => 'REGIONNAME',
            '#weight' => '-10',
            '#theme_wrappers' => array('region'));
    }
}

Note

Mohammads answer and explanation how it works in Omega was great, but his suggested solution set the $page["REGIONNAME"] to an empty array, which was not enough (at least for my 7.24 installation, maybe it worked in older versions)

0

Here is the solution I went with to force a region to display

function THEMENAME_page_alter(&$page) {
  foreach (system_region_list($GLOBALS['theme'], REGIONS_ALL) as $region => $name) {
      if (in_array($region, array('REGIONNAME'))) {
        $page['branding'] = array(
                  '#region' => 'REGIONNAME',
                  '#weight' => '-10',
                  '#theme_wrappers' => array('region'),
        );
      }
    }
  }
2
  • 1
    This replaces the region in $page with a hardcoded array, but there's no check in place to see if there's anything in that region before it overwrites it. So wouldn't this create an empty region all the time even if you placed something in it?
    – Beebee
    Commented Jun 10, 2012 at 13:13
  • Did you see how Omega does this? Commented Jul 14, 2012 at 20:45

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.