My use case:

  1. Anonymous user submits information via an entityform.
  2. The form data ($_POST) is cached and a modal dialog box displays with a login form.
  3. 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...

$entityform = entityform_empty_load('my_eform_type');
$form = entityform_form_wrapper($entityform);

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

$values = array(
  'type' => 'my_eform_type',
  'uid' => $user->uid,
  'created' => time(),
  'changed' => time(),
  'language' => 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->save();

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

This question was asked last year #1671060: programmatically submit forms with drupal_form_submit, but the author didn't need the form support. There's some code in the d.o. handbook, but it doesn't talk about drupal_form_submit().

Comments

tedbow’s picture

Why not just let form submit process go ahead as normal but cache the EntityForm ID of the newly created submission. Then after the user logs in set the UID on the entityform to the UID of the new user.

Then set up a cron job to delete entityforms submission for this type that were made by anonymous and are over say an hour old. That way you get rid of submissions for users who never ended up signing in.

You would still need some custom code but won't to have interfere with the Form API bit.

dkingofpa’s picture

If I let the entityform submit process go ahead as normal, an entityform notification email is sent and the page is redirected to the confirm page. I don't want those things to happen prior to the user authenticating.