Drupal how to redirect node form after form submission

10,076

Solution 1

If all you want to do is change where a user redirects to after they submit an add node form, from a specific link, there is a much easier way.

Just make your link look like this:

/node/add/[CONTENT-TYPE]?destination=[URL-REDIRECT]

Here is an example that I got working:

/node/add/ic-competencies-toolkit-codes?destination=admin/survey-codes

Solution 2

This worked for me:

function mymodule_form_FORM_ID_alter(&$form, $form_state){
    $form['actions']['submit']['#submit'][] = 'mymodule_redirect_callback';
}

function mymodule_redirect_callback($form, &$form_state){
    $form_state['redirect'] = '_path_';
}

An important point to note is:

$form['actions']['submit']['#submit'][] = 'some_function';

will work, but

$form['#submit'][] = 'some_function';

will not

Solution 3

You can use hook_form_alter() to do that.

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
  if ($form_id == "CONTENT_TYPE_node_form") {
    $form['#redirect'] = "node";
  }
}

Hope this works.

Solution 4

Alternatively, if you do not wish to write code, try the Rules Module: http://drupal.org/project/rules

Add a new rule and set the "React on Event" to be "After saving new content".

Set the action to be: "System: Page Redirect" and fill the fields out appropriately.

If you wish to get this rule into code, they can be exported down to a module!

Solution 5

Simplest way to redirect comment form or node form

case 'my_node_form':
        $form['#action'] .= '?destination=well-done';
      break;

Please note the "." after $form['#action']

You need to append not replace!

Share:
10,076
rocanroldani
Author by

rocanroldani

Full Stack Developer

Updated on June 04, 2022

Comments

  • rocanroldani
    rocanroldani almost 2 years

    On Drupal 7 when I post a node I redirect to the specific node created.

    I'm searching to redirect to the main admin page when I post correctly the node.

    I've tried to put this on template.php:

    function node_submit($form, &$form_state) {
      $form_state['redirect'] = 'admin';
    }
    

    But there was an error on submit:

    Fatal error: Cannot redeclare node_submit() (previously declared in /var/www/XXX/modules/node/node.module:1004) in /var/www/XXX/sites/all/themes/XXX/template.php on line xx