mirror of
https://github.com/moodle/moodle.git
synced 2025-06-03 22:55:39 +02:00
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
This commit is contained in:
parent
0adf53f6cd
commit
66c7e47b27
@ -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; $n<count($pageblocks[$position]);$n++) {
|
||||
$instance = &$pageblocks[$position][$n];
|
||||
if(!$instance->visible) {
|
||||
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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user