3

It looks like silly question but I'm really confused about redirection after node save and update.

I tried to set $form['#redirect'] - without any success. I tried to set some other vars for instance: $_REQUEST['destination'] or in $form['buttons']['submit']['#submit'] setting function name to call that sets $form_state['#redirect'] but none of this solution works.

Can you give me some source that explains node forms redirecting in D7?

  • I've just seen drupal.stackexchange.com/a/11306/7184 - it seems some other module destroys my rediretcs. – kobylecki Jun 20 '12 at 8:39
  • 2
    This does my head in fairly often too; I usually just go 'old-school' and redirect the page manually: header('Location: /new-page'); drupal_exit();. It's not the best but it does get the job done – Clive Jun 20 '12 at 8:49
  • I see your point but I'm not convinced in 'old-school' solutions. They break rules of good development. But saying it in Drupal world... sic! Thanks for advice I'll search for a time for prettier solution a I'm hope I won't should go back to this. :) – kobylecki Jun 20 '12 at 10:32
  • Using PHP functions in Drupal is certainly not breaking any rules of development. If you look at the drupal_goto() function, all it does is internally set headers. If anything you're optimising your code by removing an extra function call overhead :) – Clive Jun 20 '12 at 11:34
2

1.Use hook_node_create and hook_node_update, judge your node type to drup_goto, like:

function hook_node_create ($node) {
  if ($node->type == 'YOUR_NODE_TYPE') {
     drupal_goto('YOUR_DESTINATION');
  }
}

2.Use hook_form_alter change your node form, like:

function hook_form_alter (&$form, &$form_state, $form_id) {
  if ($form_id == 'YOUR_NODE_FORM_ID') {
    $form['actions']['submit']['#submit'][] = 'YOUR_NODE_FORM_ID_ls_submit';
  } 
}

function YOUR_NODE_FORM_ID_ls_submit ($form, &$form_state) {
  $form_state['redirect'] = 'YOUR_DESTINATION'; // Not $form_state['#redirect']
}
  • Problem is neither of these methods will work if the destination parameter is set in the URL. That trumps everything and you have to reset the static cache if you want to override it – Clive Jun 20 '12 at 9:21
  • The second way is working! Thanks a lot! destination parameter isn't my concern. – kobylecki Jun 20 '12 at 10:38
  • So, according to first method, I used it with hook_node_delete and noticed that redirection works but node isn't deleting anymore. Do you have any solution regarding deletion? – kobylecki Jun 20 '12 at 10:56
  • 1
    @kobylecki You should not use drupal_goto() on any node hook, or you are interrupting the action being done. – kiamlaluno Jun 20 '12 at 15:03
  • In hook_node_create and hook_node_update, shhould be node has been created and updated. – Rylyn Jun 21 '12 at 1:03
2

You have to add a new submit callback to your validate button with a hook_form_alter and add a $form_state['redirect'] :

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

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

Don't forget to pass the $form_state variable in your callback BY REFERENCE ( '&$form_state' and not juste '$form_state').

I used this method hundreds of times.

Finally, don't forget that a 'destination' parameter in the URL will always override any redirect set in $form_state['redirect']. See drupal_redirect_form for more information

0

There is the Node Save Redirect module for this purpose. Check it out! Here is a quote about it (from the module's project page):

... provides a method of specifying a location to take the user after saving a piece of content.

A set of radios buttons are added to the submission vertical tab on the content type edit form.

The available options are:

  • Default - Default behaviour
  • Return to Edit page after saving
  • Return to content overview page (assuming the user has access permissions
  • Re-redirect to another location. (any valid drupal path)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.