I don't think that the Entity Plus wrappers will work for a form because a form isn't an Entity. I see a bunch of functions in the form.inc API reference that look plausible ... but none jumps out at me as the right one to use.

In any case, the module I'm customizing just uses code like this:

// Note: $field = form field name.

$form[$field]['und']['0']['value']['#default_value'] = $some_value;

I don't mind doing this if this is the right way to go about it, but I still have a question. What if it is a multi-valued field, and I need to set more than one value in it? How do I do that? Would it just be something like:

foreach ($values as $some_value) {

  $form[$field]['und']['0']['value']['#default_value'][] = $some_value;

}

Thanks.

Accepted answer

Hi leeksoup. Yes, that's the correct way to set the default value of form elements. You can't use metadata wrappers, as you said, since those are not entities. 

So, for most Field API field types you'll need to do what you had, but it's "more correct" to use the constant LANGUAGE_NONE instead of 'und'

$form[$field][LANGUAGE_NONE][0]['value']['#default_value'] = 'Hello';

And the "0" should be a number, not a string. Also notice: no quotes around LANGUAGE_NONE.

As for multivalue fields, the "0" is actually the key for those, so you would do something like:

 

for ($i = 0; $i < [CARDINALITY_OF_THE_FIELD]; $i++) {
  $form['field_my_multiple_field'][LANGUAGE_NONE][$i]['value']['#default_value'] = [SOME_VALUE];
}

 

Most helpful answers

dpm is provided by Devel (I think you've used it before). In the case of a form structure this is what I do:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_node_form_alter(&$form, &$form_state) {
  dpm($form);
}

That allows you to see the form structure, before you start altering it.

Also, I want to add that this structure is different for different field types. For example, I was just writing a form alter hook to set the default value for a List (text) field, and the structure looks like this:

 

$form['field_category'][LANGUAGE_NONE]['#default_value'] = [VALUE];

There is no [0] nor ['value'] for this type of fields. 

to be on the safe side, you should always use dpm to check the structure before coding the value.

Comments

Hi leeksoup. Yes, that's the correct way to set the default value of form elements. You can't use metadata wrappers, as you said, since those are not entities. 

So, for most Field API field types you'll need to do what you had, but it's "more correct" to use the constant LANGUAGE_NONE instead of 'und'

$form[$field][LANGUAGE_NONE][0]['value']['#default_value'] = 'Hello';

And the "0" should be a number, not a string. Also notice: no quotes around LANGUAGE_NONE.

As for multivalue fields, the "0" is actually the key for those, so you would do something like:

 

for ($i = 0; $i < [CARDINALITY_OF_THE_FIELD]; $i++) {
  $form['field_my_multiple_field'][LANGUAGE_NONE][$i]['value']['#default_value'] = [SOME_VALUE];
}

 

Also, I want to add that this structure is different for different field types. For example, I was just writing a form alter hook to set the default value for a List (text) field, and the structure looks like this:

 

$form['field_category'][LANGUAGE_NONE]['#default_value'] = [VALUE];

There is no [0] nor ['value'] for this type of fields. 

to be on the safe side, you should always use dpm to check the structure before coding the value.

@argiepiano - Very helpful -- thank you!

What is dpm, and how do I use it?

I'm sure I need it b/c some of the fields are taxonomy entity references and definitely have a different structure ... but I'm not sure what exactly.

ETA: I did look at the Devel (Krumo) view for a node that already exists, but I'm not clear on how that translates to the node add form.

dpm is provided by Devel (I think you've used it before). In the case of a form structure this is what I do:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_node_form_alter(&$form, &$form_state) {
  dpm($form);
}

That allows you to see the form structure, before you start altering it.

Wonderful, yes I have. It is a big help to see exactly the structure. :)

Thank you, @argiepiano!