I'm looking for a way to force an empty region to display. Regardless of removing the if ($page['regionname']): around print render, the region still does not display unless it's populated with block content.

I see the region name in the page array, but it's empty. The region has a template suggestion, and the template does load properly when a block is inserted into the region.. I've also removed the if statement around the $content variable... no luck.

I notice Omega does this via a theme-settings checkbox, but I'm unable to track down how it's done. I THINK it is via hook_page_alter, but not 100%.

Comments

payamspot’s picture

Two possibilities:

1. You haven't done "Clear all caches" after removing "if ($page['regionname']):"

2. The height of the region div is zero.

KrisBulman’s picture

Neither is the case. The page array shows 0 elements in the regions array unless I place a block in the region; if I place a block in the region with no content (a single space for example), then the region renders and the other elements of the template appear.. (I have passed vars from page_preprocess to preprocess_region and have printed them along with the $content var in the region template).

Since this is for a custom theme, placing a block in the region is something that I cannot rely on, I need it to always be rendered.

I believe this can be done with hook_page_alter(), however I am not sure what values to pass into $page['regionname'] array to initialize the region.

KrisBulman’s picture

OK, I was able to solve this with hook_page_alter()

function THEMENAME_page_alter(&$page) {
  foreach (system_region_list($GLOBALS['theme'], REGIONS_ALL) as $region => $name) {
    if (in_array($region, array('REGIONNAME'))) {
      $page['REGIONNAME'] = array(
                '#region' => 'REGIONNAME',
                '#weight' => '-10',
                '#theme_wrappers' => array('region'),
      );
    }
  }
}
Ben Thvedt’s picture

Thanks for your solution. Was trying to display a region in a drupal 7 site that I had overridden in a theme file but on some pages it doesn't have any blocks and then the whole region would disappear. Needed to force it to display, even if it was empty. This works. Only thing is I had to wrap the foreach loop in an if statment like so or otherwise the blocks wouldn't show up when they were there:

function THEMENAME_page_alter(&$page) {
  if (!isset($page['REGION_NAME'])) :
    foreach (system_region_list($GLOBALS['theme'], REGIONS_ALL) as $region => $name) {
      if (in_array($region, array('REGION_NAME'))) {
        $page['REGION_NAME'] = array(
          '#region' => 'navigation',
          '#weight' => '-10',
          '#theme_wrappers' => array('region'),
        );
      }
    }
  endif;
}
lzimmerman’s picture

Thanks for your solution - this helped me out after a long search.

I was using nested regions, and so added my region via:
$page['content']['content']['REGIONNAME'] = array(...)