How to reload the page after a successful Drupal AJAX Form submit?
You many run into this question, for example, if you're using complex AJAX Forms and as a result so many things change, that you need to reload the current page or form.
Typically, you'd think you need to use a special Command for this. While this might be needed in some cases and is provided by modules like https://www.drupal.org/project/ajax_command_page_reload, in many cases you don't need a certain command, but you can reload the page with Drupal Core functionalities.
You can simply use the Drupal Core RedirectCommand combined with Url::fromRoute('<current>')->toString():
protected function successfulAjaxSubmit(array $form, FormStateInterface $form_state) {
$response = new AjaxResponse();
$command = new RedirectCommand(Url::fromRoute('<current>')->toString());
$response->addCommand($command);
return $response;
}
I didn't find a case where this didn't work or had a side effect like re-submitting the form. But please let me know if it didn't work for you, and you needed a custom command.
In non-AJAX cases, you might use:
$url = Url::fromRoute('route.path');
$form_state->setRedirectUrl('<current>');
But typically you won't need to do something like that, if not working with AJAX, as the form by default returns to its current URL on submit.