embed block anywhere fails at drupal 7
It's been a week I have played around with drupal 7. With drupal 6, I used to be able to place (login) block anywhere with this:
$block = (object) module_invoke($module, 'block', 'view', $delta);
$block->module = $module;
$block->delta = $delta;
return theme('block', $block);
or this:
$block = module_invoke('user', 'block', 'view', 0);
$vars['login'] = $block['content'];
I changed the delta '0' for Drupal 7:
$block = module_invoke('user', 'block', 'view', 'login'); // I have changed from 0 to 'login' for delta at drupal 7
$vars['login'] = $block['content'];
Both result in Fatal error. Any change that I am not aware of with module_invoke? There is no specific change at http://api.drupal.org/api/drupal/includes--module开发者_高级运维.inc/function/module_invoke/7
Any hint would be very much appreciated.
hook_block($op) was changed to hook_block_op() in Drupal 7. Try:
$block = module_invoke('user', 'block_view', 'login');
$vars['login'] = $block['content'];
Or why not use the form directly:
$vars['login'] = drupal_get_form('user_login_block');
Drupal 7 keeps all objects now in render arrays until the last output, this allows for a greater degree of control over content. In which case to get a final output use the render() function. Simples.
<?php
$block = module_invoke('user', 'block_view', 'login');
print render($block);
?>
Better solution, which respects Drupal theming.
function block_render($module, $block_id) {
$block = block_load($module, $block_id);
$block_content = _block_render_blocks(array($block));
$build = _block_get_renderable_array($block_content);
$block_rendered = drupal_render($build);
return $block_rendered;
}
精彩评论