| Versions | |
|---|---|
| 4.7 – 6 | hook_block($op = 'list', $delta = 0, $edit = array()) |
Declare a block or set of blocks.
Any module can declare a block (or blocks) to be displayed by implementing hook_block(), which also allows you to specify any custom configuration settings, and how to display the block.
In hook_block(), each block your module provides is given a unique identifier referred to as "delta" (the array key in the return value for the 'list' operation). Delta values only need to be unique within your module, and they are used in the following ways:
The values of delta can be strings or numbers, but because of the uses above it is preferable to use descriptive strings whenever possible, and only use a numeric identifier if you have to (for instance if your module allows users to create several similar blocks that you identify within your module code with numeric IDs).
$op What kind of information to retrieve about the block or blocks. Possible values:
$delta Which block to return (not applicable if $op is 'list'). See above for more information about delta values.
$edit If $op is 'save', the submitted form data from the configuration form.
Most modules do not provide an initial value for 'visibility' or 'pages', and any value provided can be modified by a user on the block configuration screen.
For a detailed usage example, see block_example.module.
developer/
<?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',
);
// BLOCK_CACHE_PER_ROLE will be assumed for block 0.
$blocks[1] = array(
'info' => t('Mymodule block #2 describes ...'),
'cache' => BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE,
);
return $blocks;
}
else if ($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:
// Your module will need to define this function to render the block.
$block = array(
'subject' => t('Title of block #1'),
'content' => mymodule_display_block_1(),
);
break;
case 1:
// Your module will need to define this function to render the block.
$block = array(
'subject' => t('Title of block #2'),
'content' => mymodule_display_block_2(),
);
break;
}
return $block;
}
}
?>