Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Friday, November 2, 2012

Creating custom module in Drupal 7

Creating custom module in Drupal 7
Drupal is very rich in terms of modules. There are hundreds of modules available on Drupal official site. One just need to find the appropriate module and start playing with that module after installing it in his application. 

But sometimes, there comes a situation when developer has to develop his own module. Here I am writing the steps to develop a module which will create a web form. In this tutorial we will learn how to develop a web form, validating the data & submitting data into database. 


Step 1: Create a folder at appropriate location (drupalapplication\sites\all\modules\custom\PHR data) 
PHR data is the name of customised module. 

Step 2: Now create two files in PHR data folder. 
a. phr_data.info 
     Open .info file and add below mentioned information. 
       name, description, package, core,  version 




b. phr_data.module 
     Open .module file and create a menu hook.
  function phr_data_menu(){
    $items['phr/data/%'] = array(
       'title' => 'User PHR data collection',
       'page callback' => 'phr_data_medical_data',
       'page arguments' => array(2),
       'access arguments' => true,
       'type' => MENU_CALLBACK
       );
     return $items;
 }
after writing hook now we need to write the callback function ('phr_data_symptoms_form' as specified page callback parameter) as callback function will be used after the execution of hook.
  function phr_data_medical_data($type){
       return drupal_render(drupal_get_form('phr_data_symptoms_form'));
  }
after writing callback function for the hook, it's the time to design the form of custom module.
  function phr_data_symptoms_form(){
    $form = array();
    $form['txt_symptom_date'] = array(
       "#type" => 'date_popup',
       '#date_format'=> variable_get('date_format_short', 'm/d/Y')
       );
    $form['txt_symptoms'] = array(
       "#type" => 'textarea',
       '#required' => true,
       "#title" => t('Symptoms:')
       );
    $form['btn_submit'] = array(
       "#type" => 'submit',
       "#value" => t('Submit')
       );
     return $form;
  }
Now before you see the output of your hard work, you need to activate this module. To activate this module you need to click the module link in the top of the page or go to this location 'localhost/<app_name>/admin/modules'  and click the check box in front of PHR data module.

after checking the check box, click the submit button at the bottom of the page and then go to the module url (localhost/<app_name>/phr/data/symptoms) . 
Here is the output of your hard work..
Validation and Submission of custom form
  function phr_data_symptoms_form_validate($form, &$form_state){
     // Write your validation code here to validate the form value.
  }
  
  function phr_data_symptoms_form_submit($form, &$form_state){
     // Write your submission code here to submit the form value.
  }
Follow these steps to create custom module. The functionality of this module can be enhanced. Good Luck...

No comments:

Post a Comment