From: martinlanghoff Date: Wed, 17 May 2006 04:39:43 +0000 (+0000) Subject: blocklib: cache (and used cached) block instances properly in $pageblocks X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=66c7e47b273a9ef69dec04471411e647f30a59c3;p=moodle.git blocklib: cache (and used cached) block instances properly in $pageblocks Now blocks_have_content() caches instantiated block objects inside $pageblocks and blocks_print_blocks() uses them if available. This behaviour now matches the documentation: blocks instances are created only once, get_content() may be invoked several times. A better fix would be to cache the _output_ of the block (the ->content property) but it may bite us if any block is counting on being called twice. Discussion at: http://moodle.org/mod/forum/discuss.php?d=45867 --- diff --git a/lib/blocklib.php b/lib/blocklib.php index 34a5d6c4dd..c152fd44b1 100644 --- a/lib/blocklib.php +++ b/lib/blocklib.php @@ -224,7 +224,10 @@ function blocks_have_content(&$pageblocks, $position) { if (empty($pageblocks) || !is_array($pageblocks) || !array_key_exists($position,$pageblocks)) { return false; } - foreach($pageblocks[$position] as $instance) { + // use a for() loop to get references to the array elements + // foreach() cannot fetch references in PHP v4.x + for ($n=0; $nvisible) { continue; } @@ -235,6 +238,10 @@ function blocks_have_content(&$pageblocks, $position) { continue; } if(!$obj->is_empty()) { + // cache rec and obj + // for blocks_print_group() + $instance->rec = $rec; + $instance->obj = $obj; return true; } } @@ -263,7 +270,15 @@ function blocks_print_group(&$page, &$pageblocks, $position) { $isediting = $page->user_is_editing(); foreach($pageblocks[$position] as $instance) { - $block = blocks_get_record($instance->blockid); + + // $instance may have ->rec and ->obj + // cached from when we walked $pageblocks + // in blocks_have_content() + if (empty($instance->rec)) { + $block = blocks_get_record($instance->blockid); + } else { + $block = $instance->rec; + } if (empty($block)) { // Block doesn't exist! We should delete this instance! @@ -274,10 +289,14 @@ function blocks_print_group(&$page, &$pageblocks, $position) { // Disabled by the admin continue; } - - if (!$obj = block_instance($block->name, $instance)) { - // Invalid block - continue; + + if (empty($instance->obj)) { + if (!$obj = block_instance($block->name, $instance)) { + // Invalid block + continue; + } + } else { + $obj = $instance->obj; } $editalways = $page->edit_always();