Friday, 6 February 2015

How to create custom module in drupal in three easy steps

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
  5.  
  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
  3.  
  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.

No comments:

Post a Comment