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

| Published Date: 06 Aug 2014 | Last Updated Date: 04 Sep 2017
Total Reads: 6,443 | Last Visit: 21 Sep 2017 07:30:23 AM

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

  1. name = "mymodule"
  2. description = "Custom utility module"
  3. 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.

  1. <?php
  2. // during hook_form_alter, make sure $form & $form_state arguments are passed by reference
  3. function mymodule_form_alter(&$form, &$form_state, $form_id) {
  4.  
  5. // print form_id
  6. // drupal_set_message('<pre>' . print_r($form_id,true) . '</pre>');
  7. // drupal_set_message('<pre>' . print_r($form,true) . '</pre>');
  8.  
  9. if ($form_id == 'page_node_form') {
  10.  
  11. // D6
  12. // add additional submit handler
  13. $form['#submit'][] ='mymodule_extra_submit_handler';
  14.  
  15. // or D6
  16. // $form['buttons']['submit']['#submit'][] ='mymodule_extra_submit_handler';
  17.  
  18. // D7
  19. // $form['actions']['submit']['#submit'][] ='mymodule_extra_submit_handler';
  20. }
  21. }
  22.  
  23. // extra submit handler callback function
  24. function mymodule_extra_submit_handler(&$form, &$form_state) {
  25. // empty your destination, otherwise it will redirect to destination page
  26. // even if you set redirect = FALSE
  27. unset($_GET['destination']);
  28. $form_state['redirect'] = FALSE;
  29. }
  30. ?>

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

  1. <?php
  2. // extra submit handler callback function
  3. function mymodule_extra_submit_handler(&$form, &$form_state) {
  4. unset($_GET['destination']);
  5. $form_state['redirect'] = 'node/add/page';
  6.  
  7. // to make it universal i.e. for all content type, put node type dynamically
  8. $form_state['redirect'] = 'node/add/' . $form['type']['#value'];
  9. }
  10. ?>

Some Examples

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

  1. <?php
  2. // extra submit handler callback function
  3. function mymodule_extra_submit_handler(&$form, &$form_state) {
  4.  
  5. unset($_GET['destination']);
  6. // now add some condition
  7. // redirect page for add content only
  8.  
  9. if (arg(1) == 'add') {
  10. // D6 & D7
  11. $form_state['redirect'] = 'admin/content/node';
  12. }
  13. }
  14. ?>

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

  1. <?php
  2. // extra submit handler callback function
  3. function mymodule_extra_submit_handler(&$form, &$form_state) {
  4.  
  5. unset($_GET['destination']);
  6. // now add some condition
  7. // redirect page for add content only
  8.  
  9. if (arg(1) == 'add') {
  10. // D6 & D7
  11. $form_state['redirect'] = array('node/add/page',array('query'=>array('import'=>'yes')));
  12. }
  13. }
  14. ?>

Tags

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
Max length 128 characters
Image CAPTCHA
Enter the characters shown in the image.

About This Mania

Hasan Hafiz, Blogger at web mania ltd (www.webemania.com). He has more than 5 years experience in web development platform. He worked as a Sr. Software Developer at Canadian based outsourcing software firm, Lead Developer at countries top most VOIPswitch development company and Sr. Software Engineer at USA based outsourcing software firm . His expertise areas are PHP, MySQL, CSS, Javascript, JQuery, HTML, XML and MVC Framework Codeigniter. He has been along with Drupal from more than 4 years. You may call him Drupal Addicted.