3

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

<?php
$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...

<?php
$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.

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.

1 Answer 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, &$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->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->entityType, $bundle)) : $entity_values;
foreach ($values_excluding_fields as $key => $value) {
  $entity->$key = $value;
} 
if ($entity_info['fieldable']) {
  field_attach_submit($entity_type, $entity, $form, $form_state);
}
$entity->created = isset($entity->is_new) ? REQUEST_TIME : $entity->created;
$entity->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' => LANGUAGE_NONE)); 
$form->field_entityform_1 = array(LANGUAGE_NONE => array(array('value' => 'field_1'))); 
$form->field_entityform_2 = array(LANGUAGE_NONE => array(array('value' => 'field_2' ))); 
$form->uid = 1; // this is the form id 
entityform_save($form);

Source: sushantpaste's gist

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.