Score:0

How to check if webform has a draft submission?

cn flag

I'm using hook_webform_submission_form_alter() to prepopulate some form values. When a user saves a draft of their webform submission and comes back to it later, the code in hook_webform_submission_form_alter() is wiping out their saved values. How do I first check if there's an existing draft submission? Using Webform 6.1.

Score:0
in flag
function MYMODULE_webform_submission_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  // Check if there's an existing draft submission.
  $webform_submission = $form_state->get('webform_submission');
  if ($webform_submission && !$webform_submission->isCompleted()) {
    // Get the saved values from the existing draft submission.
    $values = $webform_submission->getData();
    // Prepopulate the form fields with the saved values.
    $form['elements']['my_field']['#default_value'] = $values['my_field'];
    // Add any additional prepopulation code here.
  }
}
codesmith avatar
cn flag
Thanks but $form_state->get('webform_submission') is null.
Vikram8888 avatar
in flag
It seems that the $form_state->get('webform_submission') value is currently null, which could mean that no webform submission has been made yet
Score:0
cn flag

Ah found it...

function MYMODULE_webform_submission_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

  $form_object = $form_state->getFormObject();
  $webform_submission = $form_object->getEntity();

  if ( $webform_submission->isDraft() ) {
    return;
  } else {

    //pre-populate fields...
  }  
  
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.