How to use the $delta argument?
Question
How do I use the $delta
argument in hook_block
?
I have tried to used one, but there wasn't any effect.
What is the correct way to do this?
Solution
/**
* Implementation of hook_block().
*/
function YOURMODULE_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Block #1 name');
$blocks[1]['info'] = t('Block #2 name');
return $blocks;
case 'view':
$block = array();
switch ($delta) {
case 0:
$block['content'] = t('Hello for block #1');
break;
case 1:
$block['content'] = t('Hello for block #2');
break;
}
return $block;
}
}
OTHER TIPS
As noted elsewhere, $delta allows one module to return many blocks. It may also be easier to understand if you keep in mind that the $delta need not be numeric. It's simply an identifier.
You use the $delta first when responding to the 'list' $op. Each key in the $block array you return will be a $delta that can be used to configure or render that specific block.
switch ($op) {
case 'list':
$block['sidebar-links']['info'] = t('Sidebar Links');
$block['footer-links']['info'] = t('Footer Links');
return $block;
What this is telling the Drupal system is that the module in question has two blocks, one of which is identified by "sidebar-links", the other by "footer-links".
This will place two blocks on the admin/build/block config page. Should you enable both blocks, they will then be rendered by calls to the 'view' $op with the identifying key as the $delta:
case 'view':
switch ($delta) {
case 'sidebar-links':
$block['subject'] = t('Sidebar Links');
$block['content'] = "super weak";
break;
case 'footer-links':
$block['subject'] = t('Footer Links');
$block['content'] = "super weak";
break;
}
return $block;
}
As an added bonus, using semantic keys for your blocks will result in easier to read CSS id's. :)
An implementation of HOOK_block() can contain multiple blocks. The $delta argument specifies to the function which block to output. Example:
/**
* Implementation of hook_block().
*/
function fightfi_block($op = 'list', $delta = 0) {
$block = array();
switch ($op) {
case 'list':
$block[0]['info'] = t('Sidebar Links');
$block[1]['info'] = t('Footer Links');
return $block;
case 'view':
switch ($delta) {
case 0:
$block['subject'] = t('Sidebar Links');
$block['content'] = "super weak";
break;
case 1:
$block['subject'] = t('Footer Links');
$block['content'] = "super weak";
break;
}
return $block;
}
} // end function fightfi_block
It is better to see the example module for all kind of drupal examples.. http://drupal.org/project/examples You will find block example over here....