Alter and redirect after saving the Drupal form

<?php

/**
 * @file
 * This is my module.
 */

use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Implements hook_form_alter().
 */
function MyModule_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == 'give here form id') {
    $form['actions']['submit']['#submit'][] = '_MyModule_redirect';
  }
}

/**
 * Set the redirect here.
 */
function _MyModule_redirect($form, FormStateInterface $form_state) {
  $form_state->setRedirect($url); //you can redirect using URL

                (Or)
  $form_state->setRedirect('route.id'); //You can redirect using routing
}

Leave a comment