How to create custom module in Drupal in three easy steps

May 05, 2012


This is a series of drupals amazing tutorial (3 parts).

Part One is a step by step tutorials on how to create custom module in drupal in amazing way
Part two depends on first part. It uses first part module. This part describes how to create custom form in drupal in simplest and most easiest way
Third part depends on second part. This part describes how to create drupal table in correct, perfect and most descriptive way

To be a drupal developer, you must know how to develop custom module. In drupal, modules are plugin that extends drupal's functionality. This tutorials guides you , how to create drupal custom module in most simplest ways. After complete this tutorials, you will be realize module develoment in drupal is fun.

creating module directory

Go to drupal modules directory i.e /sites/all/modules and create a directory named 'custom' and inside custom create another directory named 'person'. So final path of your custom module is /sites/all/modules/custom/person

Necessery Files needed

At least two files needed to create a module. These are .info and .module. So create and person.module file in person directory. Remember that file name must be match with your module name (figure:1). info file keyword meaning

Structure of .info file

<?php; $Id $ name = person description = about a person core = 6.x ?>

Additional fields of .info file

Suppose your module need php version 5.0 then add php = 5.0

Suppose you give php value to 5.6 i.e php = 5.6, then you will get an error when viewing the module papge Administer -> site building -> modules ((admin/build/modules)) (figure:2)

info file keyword meaning

Suppose your custom module will not run without enableing path module. So add dependencies[] = path . You will find addional option 'Required by: person' in module listing page in path module(figure:3)>

info file keyword meaning

So finally your file looks like <?php $Id name person description about a person core 6.x package "custom module" dependencies[] = path php 5.2 ?>

For clear understanding of .info files field such as name, description, look the following figure (figure:4) carefully

info file keyword meaning

To know more about .info file please visit Drupal site

creating menu

To create menu, you have to use drupal's hook_menu() function. Here, hook indicates your module name. So your function name will be person_menu()


Now write the following code in person.module file.
Here .module file acts as a .php file. So do not forget to use php opening tags.

Do not use php short tag i.e ?> Always use php long tag. You may want to know why ? then you must read why we avoid php short opening tag while creating php application.

// $Id: person.module

 * implements hook_menu()
function person_menu(){    
$items  = array();
$items['person'] = array(
'title' => "Person",
'page callback' => "person_personal_info"// after visit drupal6/person, person_personal_info() function is called
'access callback' => true,    // must return true, otherwise it will not visible as menu item
'type' => MENU_NORMAL_ITEM// drupal's default menu type    
'weight' => '10'// we want to display person link below in our nav menu
$items// finally, do not forget to return $items array

Be careful about Drupal menu. To know common errors during creating Drupal menu please read this tutorial.

Creating callback function

When you click person link , page callback function person_personal_info() will be fired. Lets create some basic info of a person

<?php /** * callback function for person * */ function person_personal_info(){ $output 'Name: Hasan Hafiz<br />
$output .= 'City: Dhaka<br />
$output .= 'Country: Bangladesh<br />
; return $output; } ?>

Enabling module

Go to Administer > Site building > Modules (admin/build/modules) and enable the new "person" module that you've created. You will see your module under 'custom module' fieldset. Click Save configuration button.

Now visit person link you will see your desired output (figure:5).

Module enabling and display result

If you can not see your menu link, then read common mistakes while creating menu. Hope this solves your menu related problems