From b3d898a32aab23b6096c30b5520798f28c86165f Mon Sep 17 00:00:00 2001
From: Charlouze <me@charlouze.com>
Date: Thu, 28 Mar 2013 16:38:40 +0100
Subject: [PATCH 1/2] mongodb_block_render_blocks now act like
 _block_render_blocks from block.module

---
 mongodb_block/mongodb_block.module | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/mongodb_block/mongodb_block.module b/mongodb_block/mongodb_block.module
index cc38270..e78250e 100644
--- a/mongodb_block/mongodb_block.module
+++ b/mongodb_block/mongodb_block.module
@@ -89,8 +89,16 @@ function mongodb_block_page_build(&$page) {
   $query['pages']['$in'] = $ancestors;
   $query['pages_exclude']['$nin'] = $ancestors;
   $query['region']['$in'] = array_keys($all_regions);
-  $blocks = mongodb_block_render_blocks($collection->find($query)->sort(array('weight' => 1)));
-  foreach ($blocks as $region => $region_blocks) {
+  $blocks_result = $collection->find($query)->sort(array('weight' => 1));
+  $regions_blocks = array();
+  foreach($blocks_result as $block) {
+      if(!isset($regions_blocks[$block['region']])) {
+        $regions_blocks[$block['region']] = array();
+      }
+      $regions_blocks[$block['region']][] = $block;
+  }
+  foreach ($regions_blocks as $region => $region_blocks) {
+    $region_blocks = mongodb_block_render_blocks($region_blocks);
     $page[$region] = mongodb_block_get_renderable_array($region_blocks);
   }
 }
@@ -150,7 +158,7 @@ function mongodb_block_render_blocks($blocks_result) {
       if (!isset($block->subject)) {
         $block->subject = '';
       }
-      $return[$block->region]["{$block->module}_{$block->delta}"] = $block;
+      $return["{$block->module}_{$block->delta}"] = $block;
     }
   }
   return $return;
-- 
1.7.12.2


From 2bc06c5e26e515ba53e83bcb9feb2fc734a27578 Mon Sep 17 00:00:00 2001
From: Charlouze <me@charlouze.com>
Date: Thu, 28 Mar 2013 16:40:50 +0100
Subject: [PATCH 2/2] mongodb_block_render_blocks can take as parameter an
 array resulting from a collection query or a block
 object.

---
 mongodb_block/mongodb_block.module | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mongodb_block/mongodb_block.module b/mongodb_block/mongodb_block.module
index e78250e..6fa13e0 100644
--- a/mongodb_block/mongodb_block.module
+++ b/mongodb_block/mongodb_block.module
@@ -114,7 +114,9 @@ function mongodb_block_render_blocks($blocks_result) {
   $return = array();
   foreach ($blocks_result as $block) {
     // Copy module and delta out of the _id.
-    $block += $block['_id'];
+    if(is_array($block) && isset($block['_id'])){
+      $block += $block['_id'];
+    }
     $block = (object) $block;
     // Render the block content if it has not been created already.
     if (!isset($block->content)) {
-- 
1.7.12.2

