1

I'm trying to display a webform through a module controller. The webform display works as it should but I need to update existing submission data and instead of updating the existing submission a new one gets created.

The code I use looks like this:

public function edit()
{
  $submissionId = \Drupal::request()->get('sid');
  $webform = Webform::load('eventsignup');
  $webformSubmission = WebformSubmission::load($submissionId);
  $webform = $webform->getSubmissionForm([
    'data' => $webformSubmission->getData()
  ]);

  $build['submissionForm'] = $webform;

  return $build;
}

I tried to alter it by giving the operation "edit" and setting the "sid" but that leads to a duplicate key error:

public function edit()
{
  $submissionId = \Drupal::request()->get('sid');

  $webform = Webform::load('eventsignup');
  $webformSubmission = WebformSubmission::load($submissionId);
  $webform = $webform->getSubmissionForm([
    'sid' => $submissionId,
    'data' => $webformSubmission->getData()
  ], 'edit');

  $build['submissionForm'] = $webform;

  return $build;
}

What am I doing wrong?

Thanks for your help!

1

Existing webform submissions are entities and you can use the entity form builder service to build the form:

// load the webform submission entity
$webformSubmission = WebformSubmission::load($submissionId);
// build a form for the entity
$webform = \Drupal::service('entity.form_builder')->getForm($webformSubmission, 'edit');
| improve this answer | |
  • Thank you so much! Seems I tried too hard and took the complicated way... again. – SMYR Jul 17 '18 at 14:10

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.