How to programatically submit an entityform? My use case:


Anonymous user submits information via an entityform.
The form data ($_POST) is cached and a modal dialog box displays with a login form.
The user authenticates, and the cached form data is retrieved and then submitted.


I've been able to do this with webforms, but I haven't been able to accomplish the same task for entityforms. I can get the form using the following code...

&lt;?php
$entityform = entityform_empty_load('my_eform_type');
$form = entityform_form_wrapper($entityform);
?&gt;


What I've had trouble with is creating a $form_state array with the correct structure and values from the cached $_POST that I can then pass to drupal_form_submit(). My entityform has image, date, and checkbox fields, so that adds to the complexity.

I've been able to manually create and save an entityform submission using the following code...

&lt;?php
$values = array(
  'type' =&gt; 'my_eform_type',
  'uid' =&gt; $user-&gt;uid,
  'created' =&gt; time(),
  'changed' =&gt; time(),
  'language' =&gt; LANGUAGE_NONE,
);

// For brevity's sake, I don't show adding all the fields
// from the cached $_POST to the $values array.

$entityform = entityform_create($values);
$entityform-&gt;save();
?&gt;


...but that doesn't provide the form validation and reload on error functionality. Errors such as the wrong size image being uploaded, which is caught during form submission.

When my entityform is submitted, there is also an entityform notification email sent and the user is redirected to the confirm page. I don't want those things to happen prior to the user authenticating.

There's some code in the d.o. handbook, but it doesn't talk about drupal_form_submit().

I'm using Entityform 1.1.
I don't think you can submit entityform using drupal_form_submit() as it's entity, not a standard form.

The best way is to save the entity as you did, but you are missing field_attach_submit which is necessary to perform operations on field data submitted by a form.

The syntax is:

field_attach_submit($entity_type, $entity, $form, &amp;$form_state, $options = array())


Example based on given $form_state:

$form_id = $form_state['build_info']['form_id'];
$entity = $form['#entity'] ?: $form_state['build_info']['args'][0];
$entity_type = $entity-&gt;entityType();
$entity_info = entity_get_info($entity_type);
$entity_values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
list(, , $bundle) = entity_extract_ids($entity_type, $entity);

// Copy top-level form values that are not for fields to entity properties,
// without changing existing entity properties that are not being edited by
// this form. Copying field values must be done using field_attach_submit().
$values_excluding_fields = $info['fieldable'] ? array_diff_key($entity_values, field_info_instances($this-&gt;entityType, $bundle)) : $entity_values;
foreach ($values_excluding_fields as $key =&gt; $value) {
  $entity-&gt;$key = $value;
} 
if ($entity_info['fieldable']) {
  field_attach_submit($entity_type, $entity, $form, $form_state);
}
$entity-&gt;created = isset($entity-&gt;is_new) ? REQUEST_TIME : $entity-&gt;created;
$entity-&gt;changed = REQUEST_TIME;
entity_save($entity_type, $entity);


Alternatively you can submit entity form by using entityform_save(), e.g.:

$form = new Entityform(array('type' = 'my_entitty_form', 'language' =&gt; LANGUAGE_NONE)); 
$form-&gt;field_entityform_1 = array(LANGUAGE_NONE =&gt; array(array('value' =&gt; 'field_1'))); 
$form-&gt;field_entityform_2 = array(LANGUAGE_NONE =&gt; array(array('value' =&gt; 'field_2' ))); 
$form-&gt;uid = 1; // this is the form id 
entityform_save($form);


Source: sushantpaste's gist