This is a series of drupals amazing tutorial (3 parts).
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).
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)
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)>
So finally your person.info 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
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.
<?php
// $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
);
return $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).
If you can not see your menu link, then read common mistakes while creating menu. Hope this solves your menu related problems