2

I've got a multipage webform I'm letting users edit after they submit. There are many pages, and I don't want to force the user to go all the way to the last page to save. Is there a way to submit/save the form either with a Save Draft button, or when they use the prev/next buttons? Or is there a way to programmatically save the edited form ? That would also work.

This question is about editing a completed submission, not creating a new submission.

I've tried adding the submit button to every page, but it seems to function like a next button. If I set rebuild to false, the I can exit the edit process where I want, but the changes are not saved.

I've also tried manually adding a save draft button copied from the webform submission form, but that also did not work, failing the way the submit button did.

0

I was able to get this to work like so:

In a form alter, make the Submit button visible, and change the name.

   if (stristr($form_id, 'edit_form') !== FALSE) {
    $form["actions"]["submit"]['#value'] = t('Save & Exit');
    $form["actions"]["submit"]["#access"] = TRUE;
    $form["actions"]["submit"]["#_webform_access"] = TRUE;
    // Add a custom submit function so we can redirect the edit form.
    $form['actions']['submit']['#submit'][] = 'mymodule_webform_submission_form_submit';
    $form['actions']['submit']['#validate'][] = 'mymodule_webform_submission_form_validate';

  }

Next, in the custom validation function:

  // If this is the edit form, and the submit button is the trigger, always
  // do the final submit by setting the current wizard page to the confirmation
  // page.
  $trigger = $form_state->getTriggeringElement();
  if ($trigger["#id"] == 'edit-actions-submit' &&
    stristr($form['form_id']['#value'], 'edit_form') !== FALSE) {
    $form_state->set('current_page', 'webform_confirmation');
  };

Finally, in the custom submit function:

$build = $form_state->getBuildInfo();
$callback_object = $build['callback_object'];
$entity = $callback_object->getEntity();
$source = $entity->getSourceEntity();
if (!is_null($source)) {
  $nid = $entity->getSourceEntity()->id();
  $form_state->setRedirect('entity.node.canonical', ['node' => $nid]);
  $form_state->setRebuild(FALSE);
  \Drupal::messenger()->addStatus("Your work has been saved.");
}

While this answers my question, it leaves me having to deal with potential errors from conditionally required fields affected by whatever changes the user is trying to save.

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.