简体   繁体   中英

Drupal how to redirect node form after form submission

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

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

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

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.

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!

In Drupal 7 this is the correct way:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id === 'myform_id'){
    $form['#submit'][] = '_mymodule_redirect_callback';
  }

function _mymodule_redirect_callback($form, &$form_state) {
  $form_state['redirect'] = 'http://www.google.ch';
}

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM