Webemania | How to create custom module in drupal in three easy steps

| Published Date: 28 May 2012 | Last Updated Date: 04 Sep 2017
Total Reads: 75,583 | Last Visit: 22 Sep 2017 06:39:28 AM

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 person.info 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

  1. ; $Id $
  2. name = person
  3. description = about a person
  4. 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 person.info file looks like

  1. ; $Id $
  2. name = person
  3. description = about a person
  4. core = 6.x
  6. package = "custom module"
  7. dependencies[] = path
  8. 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.

  1. <?php
  2. // $Id: person.module
  4. /**
  5.  * implements hook_menu()
  6.  */
  7. function person_menu(){
  8. $items = array();
  9. $items['person'] = array(
  10. 'title' => "Person",
  11. 'page callback' => "person_personal_info", // after visit drupal6/person, person_personal_info() function is called
  12. 'access callback' => true, // must return true, otherwise it will not visible as menu item
  13. 'type' => MENU_NORMAL_ITEM, // drupal's default menu type
  14. 'weight' => '10', // we want to display person link below in our nav menu
  15. );
  16. return $items; // finally, do not forget to return $items array
  17. }
  18. ?>

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

  1. /**
  2.  * callback function for person
  3.  *
  4.  */
  5. function person_personal_info(){
  6. $output = 'Name: Hasan Hafiz </br>';
  7. $output .= 'City: Dhaka </br>';
  8. $output .= 'Country: Bangladesh </br>';
  9. return $output;
  10. }

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



Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.