getAreaBlocksArray

Permalink
What does getAreaBlocksArray Do? Does it create a array from the data put in?

 
andrew replied on at Permalink Reply
andrew
It returns an array of block objects that have been added to that area on that page. You can then loop through those block objects and work with them programmatically. It doesn't create anything – it just reads the blocks that have been added to the page.
cody12 replied on at Permalink Reply
So if I put a array into it, then it will just read that array? Is that what you are saying?
cody12 replied on at Permalink Reply
If you dont mind can u give me a example? and explain what it is doing
andrew replied on at Permalink Reply
andrew
$a = new Area('Main');
if ($c->isEditMode()) {
   $a->display($c);
}
else {
   $blocks = $a->getAreaBlocksArray($c);
   foreach ($blocks as $i => $block) {
      $html = strip_tags($block->getInstance()->getContent());
      echo '<div class="div-' . $i . ($i ? '">' : ' active">') . $html . '</div>';
   }
}


This is a pretty simple example. If you're editing the page (isEditMode) it just displays the area like normal, allowing you to add/edit/delete blocks in it. Otherwise, it gets all the blocks that have been added to the page (the $c variable is the global page variable.) Then it loops through all the blocks, runs getInstance() on each one (which returns the specific block type's controller – in this case it's the content block. Then we run the getContent() function on each content block to return the html, and add some custom DIVs around it.

Basically, this code is taking blocks that have been added to a specific area, and adding a DIV around each one, with a dynamic class of div-0, div-1, div-2, etc...
MrLindau replied on at Permalink Reply
Hi andrew!

Is there a way to use the above code to sort/order the BlockArray by a value inside the block, lets say date or a title?
And then display the blocks in the new order?

Best regards!
MrLindau