I have a webform that opens inside a modal. Everything works fine. Then I had the problem of closing the modal after the form was submitted. I fixed that to with the help of the following link: https://www.drupal.org/project/webform/issues/3002966#comment-12794056

I have a webform handler that creates a new entity in the postSave() method. After this entity is created, I want to close the modal and redirect to the entity detail page. What is the best/correct way to do this. I could use a RedirectCommand instead of the CloseDialogCommand but how do I get the entity id in the _CUSTOM_MODULE_webform_submission_form_submit_ajax() method. Using the AjaxResponse with a RedirectCommand in the postSave() method didn't work.

Any Suggestions would be great ;-)

1 Answer 1


Yes you are so close! just move the creation of the new entity from your postSave() to right in the ajax call back


_CUSTOM_MODULE_webform_submission_form_submit_ajax(array &$form, FormStateInterface $form_state) {
 // At this point we have all access to $form_state->getValues();
 // the webform submission is all ready saved so we can use the values here.

 // Get the entity storage.
 $entity_storage = \Drupal::entityTypeManager()->getStoage('ENITYNAME');

  // Create New entity.
  $new_entity = $entity_storage->create([
   'field_whatever' => $form_state->getValue('some_field');
  // Save entity
  $new_entity_id = $new_entity->id();
  // set the url. 
  $url = "/where-ever/" . $new_entity_id;

  $response = new AjaxResponse();
  $response->addCommand(new CloseModalDialogCommand());
  $response->addCommand(new RedirectCommand($url);
  // Party.
 return $response;
  • Wow, I think that was too obvious :D Thanks for your help! That did the trick.
    – mAtZ
    Jun 7, 2019 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.