2

I'm putting a node add form on a custom page that is a menu local task tab for specific taxonomy terms, and trying to set default values for some of the elements.

I'm building the form like this:

function emtr_form($form, &$form_state) {

    module_load_include('inc', 'node', 'node.pages');
    $form = node_add('mtr');

    $form['field_shift'][LANGUAGE_NONE]['#default_value'] = 'early';
    // also tried this but it doesn't work either
    //$form[LANGUAGE_NONE]['early']['#default_value'] = true;

}

When I inspect the form array I can see:

#options (Array, 3 elements)
    early (String, 13 characters ) 23:00 - 07:00
    lunch (String, 13 characters ) 07:00 - 15:00
    late (String, 13 characters ) 15:00 - 23:00 
#default_value (String, 5 characters ) early
#after_build_done (Boolean) TRUE 

This shows my default value matches the options array key, however also shows that #after_build is done. I suspect that the default value gets set in the after build function, however if I set after build to false I seem to get caught in an endless loop and the browser crashes.

I tried adding my own custom after_build function to the form and setting the default values there, but because after_build has already been done it is never called.

How can I set default values when building a form like this? Or is there a better way to load the node add form?

EDIT: Here is a paste bin with the whole form item array

EDIT2: This is a radios field that is part of a field collection that is part of a node add form.

  • 1
    ahh, field collection, ok, more complicated – berliner Feb 11 '14 at 14:43
  • Yep, field collection makes life very hard when altering forms unfortunately. If you possibly can, dump field collection out of your project and make a custom field instead. Much easier – Clive Feb 11 '14 at 16:13
1

The assignment of the default value doesn't seem correct. I think it should be like this:

$form['field_shift'][LANGUAGE_NONE][0]['value']['#default_value'] = 'early';

See the following questions and their answers for more information:

  • Normally I would agree with you - that is the typical location of #default_value, however in the case it's different (don't know why but it is). I've updated my answer with a Pastbin of the full form item array. At the bottom of the 'und' section you can see where I've added your suggestion but it is an orphan with no sibling attributes. – Felix Eve Feb 11 '14 at 14:26
  • @FelixEve Ok, I see, try $form['field_shift'][LANGUAGE_NONE]['#default_value'] = array('early' => 'early'); – berliner Feb 11 '14 at 14:34
  • This is an options field? – berliner Feb 11 '14 at 14:35
  • 1
    No, still not set. To be honest this whole form is getting too complicated - I think I'm going to build the whole thing manually and then create / edit the node programmatically. Would still be nice to know if it's possible to do what I originally asked, or at least understand why it is not working. – Felix Eve Feb 11 '14 at 14:40
  • I'm pretty sure it is possible to do this, though it's probably cleaner and easier to use hook_field_attach_form. I don't have much experience with field collection, so I can't say if that's easy or not. – berliner Feb 11 '14 at 14:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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