hook_block
Definition
hook_block($op = 'list', $delta = 0, $edit = array())
developer/hooks/core.php, line 52
Description
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.
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.
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.
$edit If $op is 'save', the submitted form data from the configuration form.
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 string 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.
Related topics
| Name | Description |
|---|---|
| Hooks | Allow modules to interact with the Drupal core. |
Code
<?php
function hook_block($op = 'list', $delta = 0, $edit = array()) {
if ($op == 'list') {
$blocks[0]['info'] = t('Mymodule block #1 shows ...');
$blocks[1]['info'] = t('Mymodule block #2 describes ...');
return $blocks;
}
else if ($op == 'configure' && $delta == 0) {
return form_select(t('Number of items'), 'items', variable_get('mymodule_block_items', 0), array('1', '2', '3'));
}
else if ($op == 'save' && $delta == 0) {
variable_set('mymodule_block_items', $edit['items']);
}
else if ($op == 'view') {
switch($delta) {
case 0:
$block['subject'] = t('Title of block #1');
$block['content'] = mymodule_display_block_1();
break;
case 1:
$block['subject'] = t('Title of block #2');
$block['content'] = mymodule_display_block_2();
break;
}
return $block;
}
}
?> 