0

hello i have what i feel is (hopefully) an easy problem to solve. Basically i have my regions defined which is fine but i have my sidebar that i want to display on the right of the content region. if the sidebar has blocks the 'Content' region should gain a class that adjusts the width to allow the sidebar to display, if no content in sidebar then the content region should display in full width. In the page template i have this so far

<?php if ($page['sidebar_first']): ?>
         <aside id="sidebar-first" role="complementary" class="sidebar clearfix">
           <?php print render($page['sidebar_first']); ?>
         </aside> <!-- /#sidebar-first -->
         <?php endif; ?>
         <?php if (!empty($page['sidebar_first']): ?>
         <div class="content-region-sidebar-sibling">
         <?php print render($page['content']); ?>
         </div>
         <?php else: ?>
           <?php print render($page['content']); ?>
         <?php endif; ?>

yet the line for
<?php (!empty($page['sidebar_first']); ?> continues to return TRUE whether the region has content or not.. i need to know how to fix this syntax so that if my sidebar region is with blocks, the content region will receive the class assigned(which is how i'm editing the width) and will not get the class if there is no sidebar blocks active on that page. Can anyone help?

1

$page['sidebar_first'] is an empty array when you have no content in that region. So it is defined and the condition

if ($page['sidebar_first']):

is true even if you have no block in the sidebar. So

print render($page['sidebar_first'])

gets executed and, after the rendering, $page['sidebar_first'] will look something like

array(
  '#printed' => true,
  '#children' => '',
)

meaning that it's no longer empty and the condition if (!empty($page['sidebar_first']): will be met in any case. You may want to rewrite your code to something like

      <?php if (!empty($page['sidebar_first']): ?>
       <aside id="sidebar-first" role="complementary" class="sidebar clearfix">
         <?php print render($page['sidebar_first']); ?>
       </aside> <!-- /#sidebar-first -->
       <div class="content-region-sidebar-sibling">
         <?php print render($page['content']); ?>
       </div>
     <?php else: ?>
       <?php print render($page['content']); ?>
     <?php endif; ?>
  • Ok so is there a way i can check to see if there is a block in the region first then if there is, set if (!empty($page['sidebar_first']): ?? all i need is a TRUE/FALSE so i can set my call on true and render normally on false.. – WebMasterSplinter Apr 16 '13 at 18:05
  • I edited the answer to make my point clearer. – pamatt Apr 16 '13 at 18:12
  • one more question, what if there is blocks in that region and its not meant to be displayed on the page, the Criteria (!empty) will still fire as TRUE, thus still rendering the class on all pags still..any workaround for that? – WebMasterSplinter Apr 16 '13 at 18:20
  • Did you verify that? I'd say that $page['sidebar_first'] only gets populated with content relevant to the page being rendered. I can't check it right now but I will ASAP – pamatt Apr 16 '13 at 18:31
  • yes verified. i have two pages one with blocks displayed as they should and one without the blocks. on both pages the region has blocks but only one page they are displayed but yet both pages receive my css class, that should only apply to the page with blocks shown. – WebMasterSplinter Apr 16 '13 at 18:48
0

This is a bit old but i had the same problem. This is how i made it work.

  <?php if (count(block_get_blocks_by_region('sidebar_first')) > 1): ?>
   <aside id="sidebar-first" role="complementary" class="sidebar clearfix">
     <?php print render($page['sidebar_first']); ?>
   </aside> <!-- /#sidebar-first -->
   <div class="content-region-sidebar-sibling">
     <?php print render($page['content']); ?>
   </div>
 <?php else: ?>
   <?php print render($page['content']); ?>
 <?php endif; ?>

Count is > 1 because if a block exists then this array also contains an index "#sorted" with it. So this could be 0 if no block is assigned to this region or 2 or more if one or more blocks are assigned.

Also this works fine with block region demonstration which were failing when i used

!empty($page['sidebar_first']): 

or

if ($page['sidebar_first']):

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.