function hook_block_list_alter

Act on blocks prior to rendering.

This hook allows you to add, remove or modify blocks in the block list. The block list contains the block definitions, not the rendered blocks. The blocks are rendered after the modules have had a chance to manipulate the block list.

You can also set $block->content here, which will override the content of the block and prevent hook_block_view() from running.

Parameters

$blocks: An array of $blocks, keyed by the block ID.

Related topics

4 functions implement hook_block_list_alter()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

block_block_list_alter in drupal/modules/block/block.module
Implements hook_block_list_alter().
dashboard_block_list_alter in drupal/modules/dashboard/dashboard.module
Implements hook_block_list_alter().
node_block_list_alter in drupal/modules/node/node.module
Implements hook_block_list_alter().
overlay_block_list_alter in drupal/modules/overlay/overlay.module
Implements hook_block_list_alter().
1 invocation of hook_block_list_alter()
_block_load_blocks in drupal/modules/block/block.module
Loads blocks' information from the database.

File

drupal/modules/block/block.api.php, line 333
Hooks provided by the Block module.

Code

function hook_block_list_alter(&$blocks) {
  global $language, $theme_key;

  // This example shows how to achieve language specific visibility setting for
  // blocks.
  $result = db_query('SELECT module, delta, language FROM {my_table}');
  $block_languages = array();
  foreach ($result as $record) {
    $block_languages[$record->module][$record->delta][$record->language] = TRUE;
  }
  foreach ($blocks as $key => $block) {

    // Any module using this alter should inspect the data before changing it,
    // to ensure it is what they expect.
    if (!isset($block->theme) || !isset($block->status) || $block->theme != $theme_key || $block->status != 1) {

      // This block was added by a contrib module, leave it in the list.
      continue;
    }
    if (!isset($block_languages[$block->module][$block->delta])) {

      // No language setting for this block, leave it in the list.
      continue;
    }
    if (!isset($block_languages[$block->module][$block->delta][$language->language])) {

      // This block should not be displayed with the active language, remove
      // from the list.
      unset($blocks[$key]);
    }
  }
}