In a Drupal 7 site, I have created a new entity called "atomyx_portal_metadata". This entity works great in all respects. I allow people to define their own entity types (much as you can create your own node types).
While allowing someone to fill out a form to enter values for such an entity type (much like filling out the form for an article for example), I use the following code:
// Get the metadata blob for this job $metadata = atomyx_portal_metadata_load($job->mid); // Save the metadata entity in our form array so we can pick it up in the validation routine $form['metadata'] = array( '#type' => 'value', '#value' => $metadata, ); // Add form elements for all attached fields field_attach_form('atomyx_portal_metadata', $metadata, $form['container'], $form_state );
The "field_attach_form" call correctly adds all necessary form elements for editing the form, and I can validate and save those values correctly in the database afterwards. However, the form elements do not contain the default values defined in the field definition and I don't seem to be able to figure out how to correctly fill in the default values of all fields into the form.
I assume I could do this by looping over all fields myself, and trying to figure out with help of the field api what the default value for that field in the given entity-type is, but that seems like a very ... un-field-api-ish way to do it.
Is there something I'm missing? Is there a call in the field attach api that would allow me to have the fields filled in with default values correctly?
Any and all hints greatly appreciated!