A similar question has been asked before: How do you identify a form element?

I am using the Devel module and dpm($form) outputs the object nicely:


But I can't set/change the default value of a custom field. How do I target set the default value to 164 in this case?

$form['question_node_form']['field_points']['und']['0']['value']['#default_value'] = 164;

The above does not work and the points_field is blank.

EDIT (complete function):

function custom2_form_question_node_form_alter(&$form, &$form_state, &$form_id) {
    drupal_set_message("Form ID is : " . $form_id);
    $form['question_node_form']['field_points']['und']['0']['value']['#default_value'] = 164;
  • Your code looks OK. Are you sure you are declaring the form_alter function to take the $form array by reference? (hook_form_alter(&$form) ?) – AyeshK Nov 9 '15 at 9:52
  • Hey Ayesh! Thx for your suggestion. Ive added the full function. – user24957 Nov 9 '15 at 10:08

You're calling the explicit hook_form_FORM_ID_alter for a start so $form_id will never be anything except question_node_form.

However the problem is you are trying to target a child of the form array. The top level array is already question_node_form. Your code should look like this:

function custom2_form_question_node_form_alter(&$form, &$form_state, $form_id) {
    $form['field_points']['und']['0']['value']['#default_value'] = 164;

Debugging removed for clarity. Noticed we removed ['question_node_form'].

Also note you can't pass $form_id by reference (not that it matters) as it can't be changed.


  • Nitpick: you can pass $form_id by reference, it can be changed, it's just that the calling function doesn't do anything with the changes so it's a bit pointless doing so – Clive Nov 9 '15 at 10:59
  • many thanks Queenvictoria! that did the trick. – user24957 Nov 9 '15 at 11:02
  • Thanks Clive. Really? You can change it? Sounds scary! – Queenvictoria Nov 9 '15 at 19:44

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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