3

I wanted to hook node/add/patient-history form because I wanted to add ajax in that form.

Before adding Ajax I wanted to get field names correctly. So I tried this:

function ajaxify_form_patient_history_node_form_alter(&$form, &$form_state, $form_id) {
  $form['field_history_patient_id']['und'][0]['#value'] = "Hello";
  // I also tried 
  $form['field_history_patient_id']['#value'] = "Hello";
}

They are not working.

I am not sure if I am taking the field names incorrectly or missing something else.

Hook name seems to be correct because this one works:

  $form['actions']['submit']['#value'] = t('Save History');

I found some similar questions on stackexchange but with no answers.

Any advice/links appreciated very much. Thank you.

3
  • You can print_r($form) to get to know the exact field name.
    – GoodSp33d
    Commented Dec 20, 2012 at 6:35
  • That is how I got this name. But this is not working. Commented Dec 20, 2012 at 6:39
  • Yes this seems to be proper and I have updated question reasoning why it seems to be proper. Please read the update. Thank you. Commented Dec 20, 2012 at 7:18

3 Answers 3

1

I use this in my module to prepopulate the forms with data when hooking on FORM_ALTER:

$form['field_history_patient_id'][LANGUAGE_NONE][0]['value']['#default_value'] = "whatever";

But when defining the form it is enough to do :

$form['field_history_patient_id']['#default_value'] = "whatever";
3
  • Thank you drcelus. This adds the value to the field. But unfortunately it is not working for adding prefix and suffix. I tried this but not working. $form ['field_username'][LANGUAGE_NONE][0]['value']['#prefiex'] = '<div id="replace_textfield_div">'; I cant see the div and id added in firebug. It would be great help if you could think of a way to add prefix and suffix too. I could not. Thank you :) Commented Dec 20, 2012 at 9:48
  • I see a typo in your code "prefiex". Otherwise maybe you should issue a new question for this.
    – drcelus
    Commented Dec 20, 2012 at 11:40
  • Exactly ! How foolish of me !! It was a typo. Works perfectly now :) :) But I still have doubts regarding why these things work differently in different forms. Commented Dec 20, 2012 at 19:36
1

Check Why is hook_form_alter so messy in d7?.

If you have Devel module installed, you can check your fields with dpm($form) in your hook or you can use echo '<pre>'; print_r($form); echo '</pre>'.

I hope this will help you to track down your element hierarchy and find out the correct attributes.

0

You need to set the #default_value property. $form['field_history_patient_id']['#default_value']

2
  • I have tried that already. It doesnot work. Thank you for your response though. Commented Dec 20, 2012 at 6:40
  • Strange, when I test this it works fine. Try raising your module's weight manually in the system table "UPDATE system SET weight = 20 WHERE name='ajaxify_form'" Commented Dec 20, 2012 at 15:36

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.