Check if block is empty
Permalink 3 users found helpfulI 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

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 }
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
$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.
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>
<? } ?>
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
<? 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.
$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.
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!