Filter out/Remove blocks

Permalink
I do have a single page and depending on a parameter passed to it I would like to only show certain blocks that are on that page. The other ones would need to get filtered out...

What is the most elegant way to implement this?

nerdess
 
JohntheFish replied on at Permalink Reply
JohntheFish
You could try advanced permissions, setting view permissions for the blocks by user group.
xaritas replied on at Permalink Best Answer Reply
Funny thing, just today I had to figure out how to lay out blocks in rows using a grid system and discovered how to do this. There are some undocumented features on Area that can help you out here:

// if this code compiles I will be shocked but you should get the general idea.
$allTheBlocks = $myArea->getAreaBlocks($c);
$someOfTheBlocks = array();
foreach ($AllTheBlocks as $candidate) {
   if (isAreaWorthy($candidate)) {
      someOfTheBlocks[] = $candidate;
   }
}
$myArea->display($c, $someOfTheBlocks);


The key here is the undocumented method, Area->getAreaBlocks, and the undocumented second parameter to display, which takes the list of blocks to display.

For extra cool points, build your list of blocks using array_filter() and a closure.
nerdess replied on at Permalink Reply
nerdess
@xaritas: thanks so much for this, mentioning that $a->display(); takes a second parameter was the hint i needed!

:-)
plasmatic replied on at Permalink Reply
I couldn't get the current answer to work. Here was my solution:

//check if not edit mode
if(!$c->isEditMode()) {
   //get all the blocks in the area
   $page = Page::getCurrentPage();
   $blocks = $page->getBlocks('area-name');
   foreach($blocks as $block) {
      //filter out whatever blocks you want based on some criteria... here is an example:
      if($block->instance->categoryID == $catID) {
         //display the block
         $block->display();
      }
   }
}
else {
   //display everything for edit mode (that way you can add blocks in edit mode)