Score:1

Get Submission ID in ajax_callback hook / after submitAjaxForm

cn flag

I'm looking to dig up the submission ID in code that I execute in the ajax_callback hook. My code looks approximately like this:

function webform_to_datalayer_ajax_callback(&$form, FormStateInterface $form_state) {

  /** @var \Drupal\webform\WebformSubmissionForm $form_object */
  $form_object = $form_state->getFormObject();

  if ($form_state->hasAnyErrors()) {
    $response = $form_object->submitAjaxForm($form, $form_state);
  }
  else {
    $values = $form_state->getValues();
    /** @var \Drupal\webform\Ajax\WebformSubmissionAjaxResponse $response */
    $response = $form_object->submitAjaxForm($form, $form_state);

    /* Right here there should be a submission, right? 
     * So how do I find the id?
     */    
    $response->addCommand(new DatalayerCommand('form_submit', $form_object->getBaseFormId(), $values));
  }
  
  return $response;
}

I imagine that after the submitAjaxForm call the submission should have been created, so I need a way to find the sid, and add it to my Command.

Score:1
ua flag

So how do I find the id?

Try

$sid = $response->getWebformSubmission()->id();
Menno van den Heuvel avatar
cn flag
Thanks! That works exactly right. Not sure how I missed that function being right there. I blame friday afternoon, and a very tall inheritance tree.
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.