Drupal How to Redirect Node or form after submission in custom way

August 08, 2014
1K
12K


Category:
Tags:

Which questions do you have in your mind? Read this tuts and solve all these:

  • Drupal How to prevent redirection after Content has been created or form submission
  • How to redirect Drupal Form after submission in customize way?
  • How to redirect to a page after submitting a form
  • Redirect to a content page after login only from that page

If these are not your question, then write your question in comment section

Writing a tiny module to solve Drupal Redirection problem. Our custom module name is: mymodule.info

Put the following code and save it to module.info

<?phpname "mymodule" description "Custom utility module" core 6.x ?>

If you are using Drupal 7, write core = 7.x instead of core = 6.x

Put the following code in your mymodule.module file and save it.

<?php
 
// during hook_form_alter, make sure $form & $form_state arguments are passed by reference
function mymodule_form_alter(&$form, &$form_state$form_id) {
    
  
// print form_id
  // drupal_set_message('<pre>' . print_r($form_id,true) . '</pre>');
  // drupal_set_message('<pre>' . print_r($form,true) . '</pre>');

  
if ($form_id == 'page_node_form') {
     
         
// D6
         // add additional submit handler         
     
$form['#submit'][] ='mymodule_extra_submit_handler';
          
     
// or D6
     // $form['buttons']['submit']['#submit'][] ='mymodule_extra_submit_handler';     
     
     // D7
    // $form['actions']['submit']['#submit'][] ='mymodule_extra_submit_handler';     
  
}
}

// extra submit handler callback function
function mymodule_extra_submit_handler(&$form, &$form_state) {
 
// empty your destination, otherwise it will redirect to destination page
 // even if you set redirect = FALSE
    
unset($_GET['destination']);
    
$form_state['redirect'] = FALSE;  
}
?> 

Form data will be saved and Display successfull message but form field will not be reset. So, to reset form data after save, we need to load this form again.

Resetting form Data after form submission

<?php
// extra submit handler callback function
function mymodule_extra_submit_handler(&$form, &$form_state) {
    unset(
$_GET['destination']);
    
$form_state['redirect'] = 'node/add/page';

  
// to make it universal i.e. for all content type, put node type dynamically
  
$form_state['redirect'] = 'node/add/' $form['type']['#value'];
}
?> 

Some Examples

Example 01: Redirect to content list page only after adding content (Do not redirect during edit content)

 

<?php
// extra submit handler callback function
function mymodule_extra_submit_handler(&$form, &$form_state) {
    
    unset(
$_GET['destination']);
  
// now add some condition
  // redirect page for add content only
  
  
if (arg(1) == 'add') {
        
// D6 & D7
    
$form_state['redirect'] = 'admin/content/node';
  }
}
?> 

Example 02: Redirect with Query String like node/add/page?import=yes

 

<?php
// extra submit handler callback function
function mymodule_extra_submit_handler(&$form, &$form_state) {
    
 unset(
$_GET['destination']);
  
// now add some condition
  // redirect page for add content only
  
  
if (arg(1) == 'add') {
        
// D6 & D7
    
$form_state['redirect'] = array('node/add/page',array('query'=>array('import'=>'yes')));
  }
}
?> 

CATEGORIES