Community Documentation

hook_block

5 core.php hook_block($op = 'list', $delta = 0, $edit = array())
6 core.php hook_block($op = 'list', $delta = 0, $edit = array())

Declare a block or set of blocks.

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.

Since 4.7, all block properties (except theme) can be set in hook_block's 'view' operation. 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).

Parameters

$op: What kind of information to retrieve about the block or blocks. Possible values:

  • 'list': A list of all blocks defined by the module.
  • 'configure': A configuration form.
  • 'save': Save the configuration options.
  • 'view': Information about a particular block and default settings.

$delta: Which block to return (not applicable if $op is 'list'). Although it is most commonly an integer starting at 0, this is not mandatory. For instance, aggregator.module uses string values for $delta

$edit:

  • If $op is 'save', the submitted form data from the configuration form.
  • In other cases, it is ignored

Return value

  • If $op is 'list', return an array of arrays, each of which must define an 'info' element describing the block.
  • If $op is 'configure', optionally return a array containing the configuration form.
  • If $op is 'save', return nothing,
  • If $op is 'view', return an array which must define a 'subject' element and a 'content' element defining the block indexed by $delta.

The functions mymodule_display_block_1 and 2, as used in the example, should of course be defined somewhere in your module and return the content you want to display to your users. If the "content" element is empty, no block will be displayed even if "subject" is present.

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

For a detailed usage example, see block_example.module.

Related topics

▾ 23 functions implement hook_block()

aggregator_block in modules/aggregator/aggregator.module
Implementation of hook_block().
block_block in modules/block/block.module
Implementation of hook_block().
block_example_block in developer/examples/block_example.module
Implementation of hook_block().
blog_block in modules/blog/blog.module
Implementation of hook_block().
book_block in modules/book/book.module
Implementation of hook_block().
comment_block in modules/comment/comment.module
Implementation of hook_block().
forum_block in modules/forum/forum.module
Implementation of hook_block().
menu_block in modules/menu/menu.module
Implementation of hook_block().
node_block in modules/node/node.module
Implementation of hook_block().
phptemplate_block in themes/engines/phptemplate/phptemplate.engine
Prepare the values passed to the theme_block function to be passed into a pluggable template engine. Uses block properties to generate a series of template file suggestions. If none are found, the default block.tpl.php is used.
poll_block in modules/poll/poll.module
Implementation of hook_block().
profile_block in modules/profile/profile.module
Implementation of hook_block().
search_block in modules/search/search.module
Implementation of hook_block().
statistics_block in modules/statistics/statistics.module
Implementation of hook_block().
system_admin_menu_block in modules/system/system.module
Provide a single block on the administration overview page.
theme_admin_block in modules/system/system.module
This function formats an administrative block for display.
theme_block in includes/theme.inc
Return a themed block.
theme_comment_block in modules/comment/comment.module
Returns a formatted list of recent comments to be displayed in the comment block.
theme_profile_block in modules/profile/profile.module
user_block in modules/user/user.module
Implementation of hook_block().
user_login_block in modules/user/user.module
user_user_operations_block in modules/user/user.module
Callback function for admin mass blocking users.
user_user_operations_unblock in modules/user/user.module
Callback function for admin mass unblocking users.

File

developer/hooks/core.php, line 67
These are the hooks that are invoked by the Drupal core.

Code

<?php
function hook_block($op = 'list', $delta = 0, $edit = array()) {
  if ($op == 'list') {
    $blocks[0] = array(
      'info' => t('Mymodule block #1 shows ...'), 
      'weight' => 0, 
      'status' => 1, 
      'region' => 'left',
    );
    $blocks[1] = array(
      'info' => t('Mymodule block #2 describes ...'), 
      'weight' => 0, 
      'status' => 0, 
      'region' => 'right',
    );
    return $blocks;
  }
  elseif ($op == 'configure' && $delta == 0) {
    $form['items'] = array(
      '#type' => 'select', 
      '#title' => t('Number of items'), 
      '#default_value' => variable_get('mymodule_block_items', 0), 
      '#options' => array('1', '2', '3'),
    );
    return $form;
  }
  else if ($op == 'save' && $delta == 0) {
    variable_set('mymodule_block_items', $edit['items']);
  }
  else if ($op == 'view') {
    switch ($delta) {
      case 0:
        $block = array(
          'subject' => t('Title of block #1'), 
          'content' => mymodule_display_block_1(),
        );
        break;
      case 1:
        $block = array(
          'subject' => t('Title of block #2'), 
          'content' => mymodule_display_block_2(),
        );
        break;
    }
    return $block;
  }
}
?>
Login or register to post comments