1

-- 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?

6
  • Where are you setting $result ?
    – 2pha
    Commented Feb 21, 2015 at 15:22
  • Also, adding $_GET['vars'] directly to a db query is quite a large security hole. There is a Drupal database api for a reason.
    – 2pha
    Commented Feb 21, 2015 at 15:24
  • My bad. I am setting the result in foreach. Here is the code foreach($query as $result){ $form['egender']['type_options']['#values'] = intval($result->egender); } Commented Feb 21, 2015 at 16:06
  • $form['egender'] is '#type' => 'select'. why are you setting ['type_options']['#values'] ? The 'select' type will ignore 'type_options'. Are you trying to add more options to go with male and female?
    – 2pha
    Commented Feb 21, 2015 at 16:17
  • Please update your original question and explain what you are trying to achieve exactly (what form items and what values)
    – 2pha
    Commented Feb 21, 2015 at 16:20

2 Answers 2

0

If you are trying to set the default value, you need to set:
$form['egender']['#default_value'].
setting it to 1 will make female the default value.
HERE is a page that shows you the relevant form array values that an be set.

2
  • 2pha - That is where I was stuck. It is not populating the default value into the dropdown. The value remains unchanged. I checked the value of $form['egender'] using dpm() and the default_value still showing integer 1. But the dropdown value remains unchanged rather than showing "Female" What am I missing? Commented Feb 23, 2015 at 4:22
  • Make sure you "hard refresh" to check it as I think most browsers will remember the dropdown value that was selected from page to page. Make sure the values of the options are correct in the <option> tags. dpm the form after setting the default value to check the default value is set in the form array.
    – 2pha
    Commented Feb 23, 2015 at 5:49
0

The problem is solved now and this is what I did, exactly what 2pha has suggested me to do.

$form['egender']['#default_value'] = t($result->egender);

The key is, clear the cache as much as you can. I tried to see the result by clearing the cache, but nothing happened. Finally I ended up restart the server and try again, and it worked. Thanks to 2pha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.