Create block by drupal code

If you most likely want to generate a block using code / programming , it is simple develope following code for that as I explain here.

Once you know the module and the delta, It is simple for creating the block.
The trick is you need to send an object with the right parameters. The code below should get you started. Remember this code may be different in Drupal7

/**
* Load drupal Block By Delta & Module
*
* @param $strModule
* - Module Name
*
* @param $intDelta
* - Module Name
*
* @return
* - Themed Block HTML
*/

function loadDrupalBlock($strModule, $intDelta) {
$block = new stdclass(); // empty object
$module = $strModule;
$delta = $intDelta; // could also be a string

$array = module_invoke($module, 'block', 'view', $delta);
if (isset($array) && is_array($array)) {
foreach ($array as $k => $v) {
$block->$k = $v;
}
}
$block->module = $module;
$block->delta = $delta;
return theme('block', $block);
}


This is calling by this way
$staticBlockHtml = loadDrupalBlock('block', 7);

No comments: