Implementing Blocks in Drupal 6 and Drupal 7

By August 29, 2015 Drupal No Comments
d7-blocks

Drupal 6: Blocks are implemented using hook_block passing the $op parameter which can have four possible values viz.
list‘: A list of all blocks defined by the module.
configure‘: Configuration form for the block.
save‘: Save the configuration options.
view‘: Process the block when enabled in a region in order to view its contents.
<?php
//Drupal 6
/**
* Implementation of hook_block().
*/
function newblock_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) { case 'list':
$blocks[0]['info'] = t('A listing of all of the enabled modules.');
$blocks[0]['cache'] = BLOCK_NO_CACHE;

return $blocks;

case 'view':
$list = module_list();

$content = theme('item_list', $list, NULL, 'ol', array('class'=>'myclass'));

$blocks['subject'] = t('Enabled Modules');
$blocks['content'] = $content;

return $blocks;

}
} ?>

Drupal 7: Blocks are implemented using four basic hooks viz.
hook_block_info is to tell Drupal about all the blocks that the module provides..
hook_block_view is responsible for building the contents of the block. This will have the functionality code
hook_block_configure defines a configuration form for a block.
hook_block_save saves the configuration options from hook_block_configure().

<?php
// Drupal 7
/**
* Implements hook_block_info().
*/
function first_block_info() {
$blocks= array(); $blocks['list_modules'] = array(
'info' => t('A listing of all of the enabled modules.'),
'cache' => DRUPAL_NO_CACHE,
);

return $blocks;
} /**
* Implements hook_block_view().
*/
function first_block_view($block_name = '') {
if ($block_name == 'list_modules') {
$list=module_list(); 

$theme_args = array('items' => $list, 'type' => 'ol');
$content = theme('item_list', $theme_args);

$block = array(
'subject' => t('Enabled Modules'),
'content' => $content,
);

return $block;
}
}
?>

CLICK HERE FOR CONTACT US WITH ANY QUESTIONS CONTACT US