How to get blocks from another page if it has a layout?

Permalink 2 users found helpful
I have a custom template for the page_list block that shows the first few blocks of the other pages it's listing (for a basic blog-style listing page) -- see

However, now with the new Layouts feature of 5.4, I notice that if there's a layout on the page, the blocks are not retrieved when $cArray[#]->getBlocks('Main') is called.

Does anyone know how to get the blocks of a page if there's a layout on the page?


-Jordan Lev

View Replies: View Best Answer
Tony replied on at Permalink Reply
you'd have to grab that area by the sub area name. So it might be something like:

getBlocks('Main : Layout 1 : Cell 1')

or you could do it as a loop, which will be something along these lines:

$area = new Area('Main')
$layouts = $area->getAreaLayouts($c);
   foreach($layouts as $l){
      $maxCell = $l->getMaxCellNumber();
      for( $i=1; $i<=$maxCell; $i++ ){
        $cellArea = new Area($aName);
        $cellBlocks= $cellArea->getBlocks();
        $blocks= array_merge($blocks,$cellBlocks);    
jordanlev replied on at Permalink Best Answer Reply
Thanks Tony -- that got me on the right track! For future reference, though, the code needs to be modified in order to actually work -- here's what I did:
$blocks = array();
$area = new Area('Main');
$layouts = $area->getAreaLayouts($c); //returns empty array if no layouts
if ($layouts) {
   foreach ($layouts as $l) {
      $maxCell = $l->getMaxCellNumber();
      for ($i=1; $i<=$maxCell; $i++) {
         $aName = $l->getCellAreaHandle($i);
         $cellBlocks = $c->getBlocks($aName);
         $blocks = array_merge($blocks, $cellBlocks);

Some notes:
$c is the collectionObject for the page, so if you're modifying the page_list block via a custom template, you'd use $cArray[#]($cArray[0], $cArray[1], etc. -- whichever element of the $cArray you're currently working with).

If you have blocks in a layout AND blocks that are not in a layout on the same page, you'll need to use the above code to get the layout blocks and then separate code to get the non-layout blocks: $cArray[#]->getBlocks('Main');

Fortunately, the call to getBlocks DOES return the blocks in the proper order (as they're positioned on the page) -- if you have multiple columns in the layout, they'll be read left-to-right in each row (all columns in a row being read before the next row).