Check if block is empty

Permalink 3 users found helpful
Hi guys,

I need some help with this: I need to check if a block is empty (essentially the 'C' field). If the 'C' (content?) field is empty I want serve a conditional accordingly. I have tried the following unsuccessfully;
$block = Block::getByName('block name');
if ($block['c'] != '') { do this } else { do that }

Also tried isset and empty but I get the following error message: Fatal error: Cannot use object of type Block as array. Help please! Am I on totally the wrong track?

Thanks,
J

obdev
 
Mnkras replied on at Permalink Reply
Mnkras
$block is an object not an array, hence that error
obdev replied on at Permalink Reply
obdev
In other words, you don't know how to help me, right? ;-)
Mnkras replied on at Permalink Reply
Mnkras
once you have the block object, you can get the block contents and compare it to ''
obdev replied on at Permalink Reply
obdev
That does help, thanks.
jordanlev replied on at Permalink Reply
jordanlev
When you call the Block::getByName() function, it's returning a block that is outside the realm of areas and pages -- hence it has nothing to do with the $c variable that you see elsewhere in templates.

You can check if it exists by doing this:
$block = Block::getByName('block name');
if ($block && $block->bID) {
  //the block exists
} else {
  //the block does NOT exist
}
2excelprogrammer replied on at Permalink Reply
Thanks buddy..Working great
obdev replied on at Permalink Reply
obdev
Hi guys, thx so much for your comments, I figured it out! To check if an area or block is empty, here's what I've done...

function blockIsNotEmpty($blockName){
  $db = Loader::db();
  $pageObject = Page::getCurrentPage();
  $pageID = $pageObject->cID;  
  $q = $db->GetOne("SELECT cl.content FROM btContentLocal cl, CollectionVersionBlocks vb WHERE vb.bID = cl.bID AND vb.arHandle = '$blockName' AND vb.cID = $pageID GROUP BY vb.bID");
  if ((!$q) || ($q == '')){
    $hasContent = false;
  } else {
    $hasContent = true;
  }
return $hasContent;
}


I'm sure there's a way to improve on this, so please feel free to comment. It works tho, and I can serve a conditional like so:

if (blockIsNotEmpty('Area Name') == true){
  // block/area is NOT empty
}


Hope that helps someone out there!
Later,
J
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Ooohhh, I get it! Totally misunderstood your original question because your sample code was a bit misleading. What you really want to do is see if there's at least one content block with content in it in a particular area. The code you have looks reasonable to me, but for future reference here's how to do it within the C5 API:
$pageObject = Page::getCurrentPage();
$pageBlocks = $pageObject->getBlocks('Area Name');
$hasContent = false;
foreach ($pageBlocks as $pb) {
   if ($pb->btHandle == 'content') {
      $content = $pb->getInstance()->getContent();
      if (!empty($content)) {
         $hasContent = true;
         break;
      }
   }
}


EDIT: Note that this will *not* work if the area has "Layouts" in it -- that screws everything up and makes the code way complicated, but if you need that code let me know because I have it lying around somewhere.
obdev replied on at Permalink Reply
obdev
Sweet! That looks much tidier! Thx Jordan. Btw, thx for the "designer content" plugin - love it! ;)
spencerhudson replied on at Permalink Reply
spencerhudson
very useful.
mhawke replied on at Permalink Reply
mhawke
Taking this a step further, I simplified things to check if the area in question (e.g. 'Hide_If_Empty') contains any blocks whatsoever, not specifically a 'content' block. I then use this boolean ($hasStuff) to hide areas that don't contain anything. By also using the isEditMode check, the area will show up during editing so you can add blocks to the empty area.

I find this useful. Hopefully others do too.

<?
global $c;
$pageObject = Page::getCurrentPage();
$pageBlocks = $pageObject->getBlocks('Hide_If_Empty');
$hasContent = false;
foreach ($pageBlocks as $pb) {
if ($pb->btHandle != NULL) {$hasStuff = true;}
break;
}

if ($hasStuff || $c->isEditMode()) {?>
<h3>Stuff...</h3>
<div>
<?
$a = new Area('Hide_If_Empty');
$a->display($c);
?>
</div>
<? } ?>
fastcrash replied on at Permalink Reply
fastcrash
i doing this in block, not in page type area
if(!isset($c->ctHandle)){  //if single page
$b = Block::getByName('Banner1');
if( $b->bName == 'Banner1'){    // cek if  banner1 exist
   $bv = new BlockView();
   echo  $bv->render($b, 'view');
}
}

it' ussually best for single page, like login, profile, register, etc so u dont need to put it one by one.
so if someone asked, why my banner do not appear in profile page? you can just said 'just rename it to 'Banner1',
thats way, u dont need to open your code, it's just a trick :)

i think the getByName is the best tools for single page
drumrby replied on at Permalink Reply
drumrby
Thank you for your post, I have used it many times. However, I have now ran into a problem. If I try to use your code multiple places on a single page type it doesn't work. What happens is that if the top section has content then it displays all of the subsequent sections that don't have content. I hope you can help.

<?
      global $c; 
      $pageObject = Page::getCurrentPage();
      $pageBlocks = $pageObject->getBlocks('Main');
      $hasContent = false;
      foreach ($pageBlocks as $pb) {
      if ($pb->btHandle != NULL) {$hasStuff = true;}
      break;
      }
      if ($hasStuff || $c->isEditMode()) {?>
        <div class="container">
            <div class="row content">
                <div class="twelve columns">
                   <div class="content_wrap">
                  <?php


In this example, if 'main' has content, 'secondary' would display even if it didn't have content. But, if 'main' is empty and 'secondary' has content, only 'secondary' will display. Like I said, I hope you can help, this is a little above me. Thank you so much in advance.
mhawke replied on at Permalink Reply
mhawke
I think I have a typo in my original code. I think you need to replace

$hasContent=false;

with:

$hasStuff=false;

And you can remove these lines from the 'Secondary' area:

global $c; 
$pageObject = Page::getCurrentPage();


These variables are set the first time you use them and they don't need to be defined again. I don't think this is causing your issue but it makes more work for the server.
drumrby replied on at Permalink Reply
drumrby
Perfect! Thank you so much. I spent awhile trying to figure that out. Thanks for the super quick response!
vibrant replied on at Permalink Reply
Hey I just wanted to update this a bit b/c I'm finding it very Useful but the earlier versions listed don't work for GlobalArea and I've found that you don't need to declare global $c or pageObject..

Hide a GlobalArea if Empty (while displaying in Edit Mode so you can add content to it!):
<?php  
            $pageBlocks = $c->        
        getGlobalBlocks('Global_Announcement');
            $hasStuff = false;
            foreach ($pageBlocks as $pb) {
            if ($pb->btHandle != NULL) {$hasStuff = true;}
            break;
            }
            if ($hasStuff || $c->isEditMode()) {
        ?>
        <div class="row-fluid" id="announcement">
            <div id="announce_cont">
                <?php
                        $a = new GlobalArea('Global_Announcement');
                        $a->


Hide an Area if empty (While displaying during Edit Mode to add content):
<?php  
            $pageBlocks = $c->        
        getBlocks('Announcement');
            $hasStuff = false;
            foreach ($pageBlocks as $pb) {
            if ($pb->btHandle != NULL) {$hasStuff = true;}
            break;
            }
            if ($hasStuff || $c->isEditMode()) {
        ?>
        <div class="row-fluid" id="announcement">
            <div id="announce_cont">
                <?php
                        $a = new Area('Announcement');
                        $a->


Hope this helps folks!
2excelprogrammer replied on at Permalink Reply
Thanks for a nice code.