Create block through Custom Module in Drupal 7

Error message

Deprecated function: The each() function is deprecated. This message will be suppressed on further calls in _menu_load_objects() (line 579 of /home/respons9/public_html/includes/
By Hemant Bharti
Mar 27, 2015 11:32

Step 1: Setup

Create a directory in your sites/all/modules/custom_module/

Create a file within your sites/all/modules/custom_module/ with

Write your module information in .info file

name=Custom Module
description = "Custom Module"
package = Custom Module
core = 7.x
version = 7.x

;These are additional fields of .info files.

;php = 5.6 
;dependencies[] = path

Step 2: Custom Block

Write this function in your .module file

 * Implements hook_block_info.
function custom_block_info() {
  $blocks['custom'] = array('info' => t('Custom block'));
  return $blocks;

block_info : block_info is a hook. that tells the Drupal that your module provide block. hook_block_info() and for implementint it replace the hook with your module name. Like custom_block_info()

You can get more hook information from!

$blocks['custom'] : Drupal uses arrays extensively to store and pass information.

t() : Translates your text to current/given language (also adds extra security).

Step 3: Block Content

Always pass your strings through t() function. If you want to use variables, then pass them with placeholders. Like this:

 * Implements hook_block_view.
function custom_module_block_view($delta = '') {
  global $user;
  $account = $user;
  $block['content'] = t('Hello @user this is a block through your module. ',array(
    '@user' => format_username($user),
  return $block;

hook_block_view : Retun a view for your block.

$delta : Unique identifier for your block.

format_username() : Function to format a username (returns Anonymous if there is no $user->name).

Step 4: Enable Module

Go to admin/module and fine cutom module and enable it.

Now go to admin/structure/block and find custom block and assign it where you want

Now you can see something like this in your block, where you have assigned it.

Add new comment

Contact Us