Thursday, July 14, 2011

Simple Drupal Form Creation in Drupal 6

Module Hook:
function company_module_menu(){
    // Parent
    $items['company_module'] = array (
    'title' => t('Company Details'),
    'description' => t('Company Information '),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('company_module_form'),
    'access callback' => TRUE ,
    'type' => MENU_NORMAL_ITEM,
    );
    // Child
    $items['company_module/list'] = array (
    'title' => t('List of Employees'),
    'description' => t('List of Employees  Information in Company'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('company_emp_list'),
    'access callback' => TRUE ,
    'type' => MENU_NORMAL_ITEM,
    );
   
    return $items;
}
Parent :
Function Form Callback:
function company_module_form(){

    $form['company'] = array (
      '#type' => 'item',
      '#title' => t('Managing company details'),
    );

    $form['company'] = array (
      '#type' => 'fieldset',
      '#title' => t('Company Details'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['company']['name'] = array (
      '#type' => 'textfield',
      '#title' => t('Company Name  '),
      '#required' => TRUE ,
      '#default_value' => "",
      '#size' => 50,
      '#maxlength' => 45,
    );

    $form['company']['addr'] = array (
      '#type' => 'textarea',
      '#title' => t('Company Address '),
      '#required' => TRUE ,
      '#default_value' => "",
      '#size' => 200,
      '#maxlength' => 200,
    );
    $form['company']['typeofcompany'] = array (
      '#type' => 'select',
      '#title' => t('Type of Company  '),
      '#options'=>array('Software'=>'Software','Banking'=>'Banking','Finance'=>'Finance','Hardware'=>'Hardware'),
      '#required' => TRUE ,
      '#default_value' => "",
    );
    $form['company']['district'] = array (
      '#type' => 'select',
      '#title' => t('District Name  '),
      '#options'=>array('banglore'=>'Banglore','chennai'=>'Chennai','pune'=>'Pune'),
      '#default_value' => "Pune",
    );
    $form['company']['shift'] = array (
      '#type' => 'radios',
      '#title' => t('Shift'),
      '#options' => array(t('Yes'),t('No')),
    );
   $form['company']['submit'] = array (
      '#type' => 'submit',
      '#value' => 'Submit',
    );
    return $form;
}

Form Submit:

function company_module_form_submit($form_id,$form_state)
{
    $st = "";

    if ($form_state['values']['shift'] == '0')
    {
        $st= "Yes";
    }
    else
    {
        $st= "No";
    }
   
     db_query("INSERT INTO tbl_company (name,addr,typeofcompany,district,shift)
                VALUES ('%s', '%s', '%s','%s', '%s')",
                $form_state['values']['name'],
                $form_state['values']['addr'],
                $form_state['values']['typeofcompany'],
                $form_state['values']['district'],
                $st,
            );
    drupal_set_message("Employee Detail Entered Successfully");
}
Child Callback:List of Employees:
Table Theming:
function company_emp_list(){
    $header = array(array('data'=>'Name','field'=>'name','style'=>'width:30%','sort'=>'desc'),
                    array('data'=>'Address','field'=>'addr','style'=>'width:30%'),
                    array('data'=>'Type Of Company','field'=>'typeofcompany','style'=>'width:15%'),
                    array('data'=>'District','field'=>'district','style'=>'width:15%'),
                    array('data'=>'Shift','field'=>'shift','style'=>'width:10%'),
                    );
    $list_query = db_query("SELECT * FROM {tbl_company}".tablesort_sql($header));
    while($list_data = db_fetch_object($list_query)){
        $rows[] = array(array('data'=>$list_data->name),
                        array('data'=>$list_data->addr),
                        array('data'=>$list_data->typeofcompany),
                        array('data'=>$list_data->district),
                        array('data'=>$list_data->shift),
                        );
    }
    $output .= theme(table,$header,$rows);
    $form['output'] = array('#value'=>$output);
    return $form;
}

3 comments:

  1. Could u please tell me How to bring this form in to a block?

    ReplyDelete
  2. Create all those menu hook and menu call backs in a block at admin side.Then use this code that u need to place the block,

    ReplyDelete
  3. $block = module_invoke(‘menu’, ‘block’, ‘view’, 26);
    print $block['content'];

    ReplyDelete