In Drupal 7, you can implement hook_block_view_alter() to alter the content of a block defined from another module. For example, menu_block_view_alter() is an implementation of that hook done by the Menu module.
function menu_block_view_alter(&$data, $block) {
// Add contextual links for system menu blocks.
if ($block->module == 'system' && !empty($data['content'])) {
$system_menus = menu_list_system_menus();
if (isset($system_menus[$block->delta])) {
$data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
}
}
}
$data
is the array defined from the implementation of hook_block_view(), and it contains two array indexes:
- "subject": the default localized title of the block
- "content": the content of the block's body
In Drupal 7, $data['content']
could be either a string containing HTML code, or an array as accepted from drupal_render().
$block
is an object containing all the data about the block loaded from the database. You can at least count on two elements:
- "module": the name of the module implementing the block
- "delta": the identifier for the block used from the module implementing it
If you just need to alter one single block, you can consider implementing hook_block_view_MODULE_DELTA_alter(), which is the equivalent of hook_form_FORM_ID_alter()
of hook_form_alter()
. Replace MODULE with the module name, and DELTA with the identifier for the block.
The hook gets the same parameters as hook_block_view_alter()
.