-- Modifying my previous question --
This really got me. This is what I am doing right now with my module. I created a menu_hook which creates a form using a theme to create data. Another menu link which displays the saved records. When the user clicks on the Name, it should display the form with all fields populated with the corresponding data but with form fields disabled. To attain this, my module file looks like this.
<?php
function agents_menu(){
$items=array();
$items['enquiryform']=array(
'title'=>'New Enquiry',
'description'=>'Add new enquiries form',
'page callback'=>'agents_form',
'access callback'=>TRUE,
'access arguments'=>array('access content'),
'type'=>MENU_NORMAL_ITEM,
);
$items['enquirylist']=array(
'title'=>'View Enquiries',
'description'=>'Lists all the enquiries',
'page callback'=>'show_enquirylist',
'access callback'=>TRUE,
'access arguments'=>array('access content'),
'type'=>MENU_NORMAL_ITEM,
);
items['enquiry_details']=array(
'title'=>'New Enquiry',
'description'=>'Add new enquiries form',
'page callback'=>'enquiry_details',
'access arguments'=>array('access content'),
'type'=>MENU_CALLBACK,
);
return $items;
}
function agents_form(){
$form = drupal_get_form('enquiries_form');
return render($form);
}
function agents_theme(){
return array(
'enquiries' => array(
'render element' => 'form',
'template' => 'customTemplate',
),
);
}
function enquiries_form($form, &$form_state){
$form = array();
$form['#theme'] = 'enquiries';
$form['efname'] = array(
'#type' => 'textfield',
'#title' => 'First Name',
'#size' => 28,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['elname'] = array(
'#type' => 'textfield',
'#title' => 'Last Name',
'#size' => 28,
'#maxlength' => 100,
'#required' => TRUE,
);
$form['edob'] = array(
'#type' => 'date',
'#title' => 'Date of Birth',
'#date_format' => 'Y-m-d',
'#required' => TRUE,
);
$options = array('0'=>'Male','1'=>'Female');
$selected = '0';
$form['egender'] = array(
'#title' => t("Choose Gender"),
'#type' => 'select',
'#options' => $options,
'#default_value' => $selected,
);
$form['submit_btn'] = array(
'#type'=>'submit',
'#value'=>'Submit',
);
return $form;
}
function enquiries_form_validate($form, &$form_state){}
function enquiries_form_submit($form, &$form_state){
$dobarr = $form_state['values']['edob'];
$dob = $dobarr['year'].'-'.$dobarr['month'].'-'.$dobarr['day'].' 00:00:00';
db_insert('enquiries')->fields(array(
'efname'=>$form_state['values']['efname'],
'elname'=>$form_state['values']['elname'],
'edob'=>($dob),
'egender'=>$form_state['values']['egender'],
'active' => 1,
)
)->execute();
drupal_set_message($form_state['values']['efname']);
}
function enquiry_details(){
$query = db_query("SELECT * FROM {enquiries} WHERE eid='".$_GET['vars']."'");
$form = drupal_get_form('enquiries_form');
foreach($query AS $result){
$form['efname']['#value']=t($result->efname);
$form['efname']['#attributes']['readonly'] = 'readonly';
$form['efname']['#attributes']['class'][] = 'readonly';
$form['elname']['#value']=t($result->elname);
$form['elname']['#attributes']['readonly'] = 'readonly';
$form['elname']['#attributes']['class'][] = 'readonly';
$form['egender']['#default_value'] = '1';
$form['egender']['#disabled'] = TRUE;
$form['egender']['#attributes']['readonly'] = 'readonly';
$form['edob']['#value'] = date($result->edob, '00:00:00');
}
return $form;
}
No matter what I do, select drop-down doesn't populate "Female". I checked the value of $form['egender']['#default_value'] before and after setting it to 1. Though the value is returned, select drop-down won't change as per the default_value. My understanding is that the form element is not refreshed after setting the value. Tried using the same approach in hook_form_alter but of no use. Same way, date is also not getting populated. What and where am I doing wrong?
foreach($query as $result){ $form['egender']['type_options']['#values'] = intval($result->egender); }