1

I have a form embedded in a block in a node; when submitting the form, it has to do two things:

  1. Open another new window to a custom route
  2. Reload the current page

The first point works correctly, but refreshing the current page is not working. Here is a snippet of the code:

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this->t('Join'),
  ];

  $form['#attributes'] = ['target' => '_blank']; // Open new window.
}

public function submitForm(array &$form, FormStateInterface $form_state) {
  // Open new window.
  $form_state->setResponse(new TrustedRedirectResponse($url));

  // Reload the current page.
  $request = $this->getRequest();
  $this->redirect($request->getUri());
}

I tried to use onclick="return location.reload();" within the button tag but it fires before submitting the form.

Any ideas?

2
  • its not a good solution but maybe adding a timeout could do the trick e.g onclick="setTimeout(function(){location.reload();},1000)"
    – john Smith
    Aug 3, 2022 at 10:45
  • Thanks for the comment; like you, I'm not a big fan of this approach, and I wonder if there is a solution using the submit handler. Aug 3, 2022 at 14:09

0

Your Answer

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

Browse other questions tagged or ask your own question.