Amount of Blocks within an Area - On each Block instance

Permalink
Is there a way to check on the amount of blocks within an area.

I would like to be able to add a block and have a public/global variable keep track of its instance in order to keep track of the amount on a page.

If Amount of Blocks on a page is divisible by two, echo a clear tag,

as an example
CusomBlock - instance1 apply class left
CusomBlock - instance2 apply class right
echo Clear Div
CusomBlock - instance3 apply class left
CusomBlock - instance4 apply class right
echo Clear Div
CusomBlock - instance5 apply class left


I could do this with jQuery but want to try and do it within php if I can
I know I can use the $i%2==0 to check for a second instance so I believe something like this could work but I don't think I am applying it correctly any help would be appreciated

If anyone would like to see the page, I am working on to get a good indication of what I am after please pm me but I don't think I am able to post the link here currently
/*
//Thinking out loud here so function calls may be incorrectly spelt
//Also this could be completely wrong
//I just wander if this is possible to do
//This way I could allow the user to add blocks to the page 
//have them sorted left and right automatically
//without needing to do it from one block
*/
//not sure which is better to use here? if any
function on_page_view() | on_start(){
  if (!defined('CUSTOMBLOCK_COUNT')) {
    Config::getOrDefine('CUSTOMBLOCK_COUNT',0);
  }
}
//Check on_view the amount of pre-existing blocks on the page and add to a public counter


Normally I use this within the controller to get the amount of blocks within a page but dont know if in this instance it will do what I require? any thoughts on the matter would be gratefull

I don't want to have to split the page into a layout or have the client add class left/right

$blocks=Blocks->getByArea('Main');

foreach($blocks as $block){
if $block->getByName('custom_block'){
//do stuff
}
}

Thanks Sean

TheRealSean
 
TheRealSean replied on at Permalink Reply
TheRealSean
I think I have managed to achive this using Globals over Constants as having trouble redefining the constant. my code so far is

public function on_page_view() {
      if(!defined('PRODUCT_COUNT')){
         define('PRODUCT_COUNT',0);
         global $pc;//product_count
         $pc = 0;
            $html = Loader::helper('html');            
            $bv = new BlockView();
            $bv->setBlockObject($this->getBlockObject());
            $this->addHeaderItem($html->css($bv->getBlockURL() . '/fancybox/fancybox.css'));
            $this->addHeaderItem($html->javascript($bv->getBlockURL() . '/fancybox/fancybox.js'));
         $this->set('drCode',"
         <script type=\"text/javascript\">
         $(document).ready(function(){
            $('a[rel=\"lightbox\"]').fancybox({});
         });

On the Page view
global $pc;
   $pc = $pc+1;
      if($pc%2==0){$this->set('class',"right");}else{$this->set('class',"left");}


then on the view

I was able to say
<?php 
if($drCode!="" && $pc <= 1){
echo $drCode;
}
?>


Not sure if this is the best way to do it but it seems to work I have multiple blocks on the page floated left and right and each one is able to use the same fancybox js.

Though if anyone can clean this up or suggest a better way of doing it please let me know

Thanks
Sean
jordanlev replied on at Permalink Reply
jordanlev
I think what you want is this:
$a->getTotalBlocksInArea($c)

(that assumes the area in question has been assigned to the variable $a).

Note that there is currently a bug in Concrete5 that prevents this from working when there are Layouts on the page. See these for details and a fix:
http://www.concrete5.org/community/forums/customizing_c5/using_gett...
http://www.concrete5.org/community/forums/usage/layout-makes-blocks...