Problem/Motivation

I want to create a form which redirects after submit. I want to use the "post book" example as a model. However, the redirect in the "post book" example has no effect.

Steps to reproduce

  1. Create a fresh Drupal install
  2. Install Multistep Form Framework and Multistep Form Framework Examples
  3. Open /post-book.
  4. Complete steps, entering data into each field
  5. On the "Congratulations" page, select the "Go Back to Site" button"

Result

Expected: Return to the front page.
Actual: Remain on the "Congratulations" page.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

chipcleary created an issue. See original summary.

chipcleary’s picture

I've been able to get this to work by taking the following steps.

  1. Override the ajax callback function inside Congratulation.php:
        protected function getSubmitButton(): array
        {
            $button = parent::getSubmitButton();
            $button['#value'] = $this->t("Go back to site.");
            $button['#ajax']['callback'] = '::ajaxRedirectOnSubmit';
            $button['#ajax']['event'] = 'click';
            return $button;
        }
    
        function ajaxRedirectOnSubmit()
        {
            $response = new AjaxResponse();
    
            $url = Url::fromRoute('<front>');
            $command = new RedirectCommand($url->toString());
            $response->addCommand($command);
    
            return $response;
        }
    
  2. Adjust the getActions method of BaseStep to not reset the ajax callback which I've now overridden.
            if ($this->wizard->isAjaxEnabled()) {
                foreach (Element::children($actions) as $child) {
                    // do not set ajax if it is already set (i.e., overridden)
                    if (!isset($actions[$child]['#ajax'])) {
                        $actions[$child]['#ajax']['callback'] = '::ajaxSubmit';
                        if (isset($form['#id'])) {
                            $actions[$child]['#ajax']['wrapper'] = $form['#id'];
                            $actions[$child]['#ajax'] += $this->getDefaultAjaxOptions();
                        }
                    }
                }
            }
    

Hope this helps.

chipcleary’s picture

  • dinazaur authored a76ecb3b on 2.x
    Issue #3330886 by chipcleary: Redirect on submit does not work in...
dinazaur’s picture

Fixed issue in the examples module.

dinazaur’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.