[Drupal] How to create custom block in Drupal

By varokky.jose | 30th April 2015 | 1 min read

Blocks are the boxes visible on screen.Sometimes it becomes necessary to create custom blocks on site for showing contents.This can be easily implemented by using hooks in drupal.

In order to create a block we have to make it selectable in the block configuration area,for this we can use hook_block_info() function.Refer the example below.


function hook_block_info() {
	 $blocks['content'] = array(
    'info' => t('content'),
    'cache' => DRUPAL_NO_CACHE,
		'region' => 'sidebar_first',
  );
	return $blocks;
}

The block can be made visible using hook_block_view() function.This function returns a renderable view of block.


function hook_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'content':	
		  $block['subject'] = t('Statistics');
		  $block['content'] = get_contents();
		break;
	}
	return $block;
}

Here the get_contents() is a custom function which will return the value to be displayed on the block.

Hope this help.