4

I can redirect user after adding a node to any another page but would like to redirect them to previous page they were linked from. Now I use the following code:

function user_add_node_form_foto_node_form_alter(&$form, &$form_state, $form_id) {
  $form['actions']['submit']['#submit'][] = 'user_add_node_foto_node_form_submit'; 
}

function user_add_node_foto_node_form_submit($form, &$form_state) {
  $form_state['redirect'] = 'node/add/foto';
}

How can I change a code to redirect users to previous page after submitting the form?

EDIT: Every foto node type has a node reference field "field_relate_to_gallery". By using this I created a gallery in Views and added a link "Add foto in gallery" on gallery page linking to add node form. I would like returning users back to gallery after adding a node "foto". That means that a have a lot of galleries with different ID and a cannot redirect the this form only to one particular gallery.

My Solution If someone is interested in. I did it by using node reference from url module. Now my user can add an image from gallery page (there is a link to add node form) and after submitting it he returnes to the gallery page.

7

You want drupal_get_destination(). This function creates a 'destination' query parameter suitable for use in l() that will redirect the user back to the page they came from after submitting the form. So, when you create your link to add a new node, use the l() function along with drupal_get_destination(). For example...

l('Add new foto', 'path/to/add/a/foto', array('query' => drupal_get_destination()))

That should do it.

  • 1
    drupal_get_destination() works very well with drupal_goto() (without arguments) – André Laszlo Oct 3 '13 at 11:34
2

there are 2 possibility:

  1. use $_SERVER['HTTP_REFERER']
  2. add something to the current page url (like a "from=XYZ") and use it.

Remember that some browser does not set $_SERVER['HTTP_REFERER'] and if a user browses directly to the page, no referer header will be present. So be sure to have a fallback in place such as not showing the back link, or linking to a default start page or something other.

  • Thank you for suggestions, I will try. I tried $form_state['redirect'] = 'fotopage/'. $field_relate_to_gallery -> field_relate_to_gallery_nid; but recieved an error undefined variable field_relate_to_gallery – moto Sep 20 '12 at 12:16
0

I hope this get you idea about how you can achieve it, (save pervious url in somewhere(variable, session,static variable etc ) and in submit function redirect to it .

 function user_add_node_form_foto_node_form_alter(&$form, &$form_state, $form_id) {
    global $user;
    variable_set('perUrl'.$user->uid,$_SERVER['HTTP_REFERER']);
    $form['actions']['submit']['#submit'][] = 'user_add_node_foto_node_form_submit'; 

    }
function user_add_node_foto_node_form_submit($form, &$form_state) {
global $user;
$form_state['rebuild'] = TRUE;
$form_state['redirect'] = variable_get('perUrl'.$user->uid);
}
  • variable_get() would return the same value to every user. – kiamlaluno Sep 20 '12 at 8:03
  • Use a variable will be a creazy idea, it will need a variable for every user, and every place where the link is. – Oskar Calvo Sep 20 '12 at 8:05
  • @OskarCalvo ,look at it again, here is no where expalin every thing in details, your scope is very short.users in here ask question and get idea from answer not exact the answer, I said save variable as example in variable on session or static and etc. – Yusef Sep 20 '12 at 8:19
  • @kiamlaluno,thanks for your note, but I said this for just get idea from it ( maybe session be more better ) – Yusef Sep 20 '12 at 8:20
-1

Try with this code:

//Custom submit handler
function _custommade_custom_submit($form, &$form_state) {
   $form_state['rebuild'] = TRUE;
   $form_state['redirect'] = 'whatever';
}

Also, you can use a "destination in the link to the form like this: /domain.com/mipath?destination=anotherurl

look at:

http://drupal.org/node/1361246#comment-5325508

also:

http://api.drupal.org/api/drupal/includes%21common.inc/function/l/7#comment-13259

Oskar

  • read question again – Yusef Sep 20 '12 at 7:51
  • If you set $form_state['rebuild'] to TRUE, the user is redirected to the form, not to a different page. – kiamlaluno Sep 20 '12 at 8:50

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.