Persist all query string parameters during user login

Public

Keep all query string parameters during user login form submission when a destination is set. In the example, "http://example.com/user/login?destination=node%2F456&foo=bar&data=baz", the user logs in, and directed to "node/456?foo=bar&data=baz".

Put this code in an implementation of hook_form_alter.

Get raw version
php
  1. // If we're at the login page, and there is a destination, save any other
  2. // query parameters in the URL.
  3. if ($form_id == 'user_login' && isset($_GET['destination'])) {
  4. // Aguments passed to drupal_goto. See Form API docs about #redirect.
  5. $args = array();
  6. $args[] = $_GET['destination'];
  7. $params = $_GET;
  8. unset($params['q'], $params['destination']);
  9. if (count($params) > 1) {
  10. // Add other get params to arguments passed to #redirect.
  11. $args[] = $params;
  12. }
  13. $form['#redirect'] = $args;
  14. }