hook_block_info

Versions
7
hook_block_info()

Define all blocks provided by the module.

Any module can export a block (or blocks) to be displayed by defining the _block hook. This hook is called by theme.inc to display a block, and also by block.module to procure the list of available blocks.

After completing your blocks, do not forget to enable them in the block admin menu.

For a detailed usage example, see block_example.module.

Return value

An associative array whose keys define the $delta for each block and whose values contain the block descriptions. Each block description is itself an associative array, with the following key-value pairs:

  • 'info': (required) The human-readable name of the block.
  • 'cache': A bitmask of flags describing how the block should behave with respect to block caching. The following shortcut bitmasks are provided as constants in common.inc:

    • DRUPAL_CACHE_PER_ROLE (default): The block can change depending on the roles the user viewing the page belongs to.
    • DRUPAL_CACHE_PER_USER: The block can change depending on the user viewing the page. This setting can be resource-consuming for sites with large number of users, and should only be used when DRUPAL_CACHE_PER_ROLE is not sufficient.
    • DRUPAL_CACHE_PER_PAGE: The block can change depending on the page being viewed.
    • DRUPAL_CACHE_GLOBAL: The block is the same for every user on every page where it is visible.
    • DRUPAL_NO_CACHE: The block should not get cached.
  • 'weight', 'status', 'region', 'visibility', 'pages': You can give your blocks an explicit weight, enable them, limit them to given pages, etc. These settings will be registered when the block is first loaded at admin/block, and from there can be changed manually via block administration. Note that if you set a region that isn't available in a given theme, the block will be registered instead to that theme's default region (the first item in the _regions array).

Related topics

Code

modules/block/block.api.php, line 54

<?php
function hook_block_info() {
  $blocks['exciting'] = array(
    'info' => t('An exciting block provided by Mymodule.'),
    'weight' => 0,
    'status' => 1,
    'region' => 'sidebar_first',
    // DRUPAL_CACHE_PER_ROLE will be assumed for block 0.
  );

  $blocks['amazing'] = array(
    'info' => t('An amazing block provided by Mymodule.'),
    'cache' => DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE,
  );

  return $blocks;
}
?>
Login or register to post comments
 
 

All source code and documentation on this site is released under the terms of the GNU General Public License, version 2 and later. Drupal is a registered trademark of Dries Buytaert.