0

I need to save a draft of the webform after each field change. I know that you can save a draft by clicking a button (or between pages in multi-page forms) and my initial tough was to do a form_alter, add an ajax callback to fields and call the submit handler for save_draft. Had anyone tried something like this?

Some conditions:

  • If there is an existing draft for the current user, it needs to update that one (not create a new one);
  • it needs to work for anonymous users as well.

Drupal 7 + Webform 7.x-3.x

I'll post the answer here if I manage to do it, but any feedback would be appreciated if there are people who've done this before.

0

Ok, I've found this http://drupal.org/node/1319926 and based on it I've managed to make it work. This is my ajax callback (it can be attached using hook_form_alter(), based on your needs):

function _my_module_js_draft($form, &$form_state) {
  global $user;
  $draft_sid = _webform_fetch_draft_sid($form['#node']->nid, $user->uid);

  $form_state['values']['details']['sid'] = $draft_sid;
  $form_state['values']['op'] = !empty($form['actions']['draft']['#value']) ? $form['actions']['draft']['#value'] : t('Save Draft');

  $form_state['save_draft'] = TRUE;
  webform_client_form_pages($form, $form_state);
  webform_client_form_submit($form, $form_state);

  return $form;
}

For having the save draft functionality available for Anonymous I've used this patch: http://drupal.org/node/1932560#comment-7185674

  • Could you provide some more detail about how you attached it using hook_form_alter? – user11312 Jun 11 '14 at 18:54
  • to your form item array add something like (sorry, I can't make this format the code correctly): '#ajax' => array( //save draft callback 'callback' => '_my_module_js_draft', 'event' => 'click', 'validate_first' => FALSE, //run the request even if validations fails 'progress' => array( 'type' => 'none', 'message' => '', ), ), – Stefan Jun 12 '14 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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