[Drupal 7] How to redirect a node-edit form to a custom page after submission?

| | 1 min read

Many Drupal users wanted to know how to redirect a node-edit form to a custom page after submission in a Drupal 7 based website. If you want to know how to redirect a node-edit form in your Drupal site to a custom page after the form has been submitted then continue reading.

It is easy to redirect a default drupal form after submitting. For this you can make use of a custom redirection function. Here I am going to explain how to redirect a node edit form to a custom page called 'Mypage'. Follow the steps below to do that.

  1. First create a hook_form_alter function.
  2. Inside this check whether the form id is your required one.
  3. Now add a submit function of your own as shown below.
    function your_module_name_form_alter(&$form, &$form_state, $form_id) {
                 
                   if (($form_id == 'your_form_id') && (arg(2)=='edit')) { //here add your form's ID
                     
                     $form['actions']['submit']['#submit'][] ='hook_node_edit_custom_submit';
                     
                   }
                 
                 }
  4. Finally define the custom submit handler as shown below
    function your_module_name_node_edit_custom_submit($form, &$form_state) { 
                  unset($_GET['destination']);//Removes the destination from the url
                  unset($_REQUEST['edit']['destination']); 
                  $link = 'MyPage';
                  $form_state['redirect'] = $link;  // This will redirect the edit form to Mypage.
                }

Hope this helps!!