I am using hook_block_list_alter and hook_block_view_alter to force caching upon a block provided by nice_menus, which has bad performance when used with my HUGE taxonomy menu. Here is an abstract of my code:
/**
* Performance hack for nice_menus
* Force the nice menu blocks to be cached globally.
* Note that the same menu will be displayed for all users on all pages where the block is enabled.
*/
function mymodule_block_view_alter(&$data, $block) {
$deltas = variable_get('mymodule_nice_menus_performance_hack_deltas',array());
if ($block->module == 'nice_menus' && in_array($block->delta,$deltas)) {
// if we are here, then the block has been rendered. set the cache.
cache_set('mymodule_nice_menu_'.$block->delta.'_data', $data, 'cache_block', CACHE_TEMPORARY);
cache_set('mymodule_nice_menu_'.$block->delta.'_block', $block, 'cache_block', CACHE_TEMPORARY);
}
}
function mymodule_block_list_alter(&$blocks) {
$deltas = variable_get('mymodule_nice_menus_performance_hack_deltas',array());
foreach($blocks as &$block) {
if ($block->module == 'nice_menus' && in_array($block->delta,$deltas)) {
$cached_data = cache_get('mymodule_nice_menu_'.$block->delta.'_data', 'cache_block');
$cached_block = cache_get('mymodule_nice_menu_'.$block->delta.'_block', 'cache_block');
if (!empty($cached_data) && !empty($cached_block)) {
$block = $cached_block->data;
$block->content = $cached_data->data['content'];
$block->subject = $cached_data->data['subject'];
} // else just don't set anything, and nice_menus will recreate it!
}
}
dpm($blocks);
}
Now my problem is, that whenever $block->content
is set, the block's weight is ignored, and it is rendered as the first block in the region, even though $block->weight
was set to the correct value.
What causes this, and how does one enforce the weight?
$block = $cached_block->data;
inmymodule_block_list_alter()
it is clearing out the$block->weight
value since that's not part of the data returned byhook_block_view()
but rather part of the information fromhook_block_info()
So, I'd try just setting$block->content
etc here without the initial reset of$block
.