1

I need the user,opening a certain page of my site (www.foo.com/page_to_visit), to login and then go to the requested page, so I set up an hook_menu for this specific page and a callback. In the callback I added this code which works

drupal_goto('user', array('query'=>drupal_get_destination()))

but if the url contains query strings (www.foo.com/page_to_visit?id=12&od=12) they are removed. I mean that, after login, the user gets redirected to www.foo.com/page_to_visit instead of www.foo.com/page_to_visit?id=12&od=12. How do I avoid this behaviour?

UPDATE: Drupl_goto has something supporting this. Produced this code, following documentation

$args = array();
$params = $_GET;
$args[] = $params['q'];
unset($params['q'], $params['destination']);
if (count($params) > 1) {
  $pippo['query'] = $params;
  $args[] = $pippo;
}
drupal_goto('user', array('query'=>$args));

$args results in --->
Array
( [0] => page_to_visit
  [1] => Array
    ( [query] => Array
            ( [id] => 12 
              [od] => 12
            )
    )
)

The result is odd for the redirect looks like http://www.foo.com/user?0=page_to_visit&1[query][id]=12&1[query][od]=12

2
  • 1
    Have you tried out the LoginToboggan module? It might handle the passing of query string after login but I can't be sure. Half the problem is that the login destination is already being passed as a query string: drupal.org/node/1671118 Commented Feb 4, 2016 at 14:58
  • Surely I'll try. The problem lies exactly there... Commented Feb 4, 2016 at 16:12

2 Answers 2

0

I think, my suggestion is :-

  1. alter login form
  2. add a hidden field for destination URL.
  3. add submit handler like $form['#submit'][] = 'destination_redirect_handler'
  4. use drupal_goto('destination_redirect_url') in this destination_redirect_handler
-1

Modify your call to drupal_goto() like this:

drupal_goto('user', array('query'=>drupal_http_build_query($args)));

drupal_http_build_query() builds a formatted query string from an array.

Hope this helps.

4
  • query must be An array of query key/value-pairs (without any URL-encoding) to append to the URL. (api.drupal.org/api/drupal/includes%21common.inc/function/url/…). Converting to a string will break it
    – Clive
    Commented Apr 11, 2018 at 10:19
  • Modifying the core drupal_goto() method is poor advice. Drupal has interfaces for doing this without hacking core. Never hack core. Commented Nov 27, 2018 at 20:55
  • Hey @ChrisBurgess, just wanted to clarify that I meant to modify the call to drupal_goto() like the one mentioned above. Thanks. Commented Nov 28, 2018 at 11:07
  • Ah, that's much better advice with your correction Supriya! Thanks for improving it. Commented Nov 28, 2018 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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