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.
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'
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: