I am writing a custom module for Drupal 7. I need to create 5 different blocks with the module. Drupal has the hook_block_info and hook_block_view hooks which create blocks. How can I add multiple blocks since these hooks allow only one block per module?
1 Answer
The following code should help. You'll obviously need to add your 5 blocks as appropriate, but it's just to show how you can add multiple blocks in one module:
function MYMODULE_block_info() {
$blocks = array();
$blocks['myfirstblock'] = array(
'info' => t('My block admin info'),
'status' => 1,
);
$blocks['mysecondblock'] = array(
'info' => t('My second block admin info'),
'status' => 1,
);
return $blocks;
}
function MYMODULE_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'myfirstblock':
$block = array(
'subject' => t('My first block title'),
'content' => t('My first block content'),
);
break;
case 'mysecondblock':
$block = array(
'subject' => t('My second block title'),
'content' => t('My second block content'),
);
break;
}
return $block;
}
Reason for status => 1 from hook_block_info() API docs:
status: (optional) Initial value for block enabled status. (1 = enabled, 0 = disabled). Most modules do not provide an initial value, and any value provided can be modified by a user on the block configuration screen.
I'm not sure where you got the one block per module idea from. You can create as many blocks as you'd like in a module.