Why does the form submit twice when I create and submit a submission with this custom code?

Created on 27 January 2023, almost 2 years ago
Updated 15 February 2023, almost 2 years ago

Problem/Motivation

I am having a webform XYZ and I want to submit data using earlier webform submission (by updating a few data) programmatically and I believe, that eventually triggers the webform handler. I found that the Webform handler called twice on WebformSubmission.

I tried 2-3 ways to submit the data but it results in the twice submission.

Steps to reproduce

Here is the basic code:

$webform = Webform::load($webform_id);
$webform_submission = WebformSubmission::load($webform_submission_id);
$submission_data = $webform_submission->getData();
$merged_array = array_merge($submission_data, $webform_new_data);
            $values = [
              'webform_id' => $webform->id(),
              'data' => $merged_array
            ];

First Try:

$webform_submission = WebformSubmission::create($values);
$webform_submission->save();

Second try:

             /** @var \Drupal\webform\WebformSubmissionForm $form_object */
            $form_object = NULL;
            $form_object = $this->entityTypeManager->getFormObject('webform_submission', 'api');
            $form_object->setEntity($webform_submission);
            // Create an empty form state which will be populated when the submission
            // form is submitted.
            $form_state = new FormState();
            // Submit the form.
            \Drupal::formBuilder()->submitForm($form_object, $form_state);

Third Try:

$webform_submission = WebformSubmissionForm::submitWebformSubmission($webform_submission);

While checking logs (in Second try), I found that the first 2 logs are as expected but 3rd and 4th show history/806/read these read calls from history.
When I hit https://abc.com/history/806/read URL in a browser, I see a new webform submission entry in /admin/structure/webform/manage/XYZ/results/submissions tab.

Jan 27 05:39:44 abc: https://abc.com|1674797983|webform|116.50.59.202|https://abc.com/article-page-having-paragraph-webform-field|https://abc.com/article-page-having-paragraph-webform-field|111|Edit|XYZ: Submission #12 created. request_id="v-111"
Jan 27 05:39:44 abc: https://abc.com|1674797983|HubSpot|116.50.59.202|https://abc.com/article-page-having-paragraph-webform-field|https://abc.com/article-page-having-paragraph-webform-field|111||Webform "XYZ" results successfully submitted to HubSpot. request_id="v-111"
Jan 27 05:40:04 abc: https://abc.com|1674798003|webform|116.50.59.202|https://abc.com//history/806/read|https://abc.com/article-page-having-paragraph-webform-field|111|Edit|XYZ: Submission #13 created. request_id="v-222"
Jan 27 05:40:04 abc: https://abc.com|1674798004|HubSpot|116.50.59.202|https://abc.com/history/806/read|https://abc.com/article-page-having-paragraph-webform-field|111||Webform "XYZ" results successfully submitted to HubSpot. request_id="v-222"

I am not sure, why submission happens twice. Any help would be appreciated!

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

💬 Support request
Status

Closed: cannot reproduce

Version

6.1

Component

Code

Created by

🇮🇳India nileema19

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024