Index: login_destination.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/login_destination/login_destination.module,v
retrieving revision 1.10.2.23
diff -u -r1.10.2.23 login_destination.module
--- login_destination.module	27 Oct 2010 22:12:46 -0000	1.10.2.23
+++ login_destination.module	21 Dec 2010 14:15:47 -0000
@@ -156,23 +156,50 @@
  * We hook here to redirect.
  */
 
-function login_destination_user($op, &$edit, &$account, $category = NULL) {
-  static $login_destination;
-  
-  // If login and not during an installation profile
- if ( $op == 'login' && !isset($login_destination)) {
-    $login_destination = true;
-    // module_invoke_all('user', $op, $edit, $account, $category); // Removed: causing other hook_user calls in other modules to be called twice. Doesn't look like this implementation is doing anything useful anyway
-    
-    $arr = login_destination_calculate_redirection_path_and_query($edit);
-    $path  = $arr[0];
-    $query = $arr[1];
-  
-    // if condition is ok and this is not the user_login form - redirect. (on the user_login form we set $form['#redirect'] which
-    // takes care of the redirection there.)
-    //if (login_destination_apply_redirect() && $edit['form_id'] != "user_login") {      
-    if (login_destination_apply_redirect($account)) {
-      login_destination_redirect_to_path_and_query($path, $query);
+/**
+ * Implements hook_form_alter
+ */
+function login_destination_form_alter(&$form, &$form_state, $form_id) {
+  // We redirect by setting the $form_state['redirect'] variable. If we simply
+  // call drupal_goto() it may break compability with other modules. If we set
+  // the $_GET['destination'] variable we will loose the possibility to redirect
+  // to an external URL.
+
+  // Please note the the system_goto_action() calls drupal_goto() so it will
+  // have priority over this module.
+
+  // If we add the $form_state['redirect'] here it will be overriden by the
+  // user_login_submit(). So we add a submit handler here and will set the
+  // redirect later.
+  switch ($form_id) {
+    case 'user_login':
+    case 'user_login_block':
+        $form['#submit'][] = 'login_destination_submit';
+      break;
+  }
+}
+
+/**
+ * Helper submit function.
+ */
+function login_destination_submit($form, &$form_state) {
+  $login_destination = true;
+  // module_invoke_all('user', $op, $edit, $account, $category); // Removed: causing other hook_user calls in other modules to be called twice. Doesn't look like this implementation is doing anything useful anyway
+
+  $arr = login_destination_calculate_redirection_path_and_query($form);
+  $path  = $arr[0];
+  $query = $arr[1];
+
+  // if condition is ok and this is not the user_login form - redirect. (on the user_login form we set $form['#redirect'] which
+  // takes care of the redirection there.)
+  //if (login_destination_apply_redirect() && $edit['form_id'] != "user_login") {
+  if (login_destination_apply_redirect()) {
+    $form_state['redirect'] = login_destination_redirect_to_path_and_query($path, $query);
+    // Hack: The $_GET['destination'] from user_login_block overrides the
+    // $form_state['redirect'] in drupal_goto(), so unset it.
+    // More on this issue http://drupal.org/node/732542.
+    if (!variable_get('ld_destination', TRUE)) {
+        unset($_REQUEST['destination']);
     }
   }
 }
@@ -188,7 +215,7 @@
     if (!empty($query) ) { $add_to_path = urlencode("?" . $query); }
     
     // redirect
-    $_REQUEST['destination'] = $path .  $add_to_path;
+    return $path .  $add_to_path;
     // drupal_goto($path, urlencode($query), NULL, 301);
     
   }
@@ -199,7 +226,7 @@
     if (!empty($query) ) { $add_to_path = "?" . $query; }
     
     //redirect
-    $_REQUEST['destination'] = $path .  $add_to_path;
+    return $path .  $add_to_path;
     // drupal_goto($path, urlencode($query), NULL, 301);
   }
 }
@@ -266,15 +293,8 @@
  *
  * @return bool TRUE - apply redirect, FALSE - not to apply redirect.
  */
-function login_destination_apply_redirect($account) {
-  global $user;  
-  // don't redirect on registration's password reset
-  // not clear if this affects anything. Can't hurt. At least the wrong redirection does not happen currently. Phew!
-  // Also check for force_password_change
-  if ((arg(0) == 'user' && arg(1) == 'reset')
-    || ($account->force_password_change && $account->uid == $user->uid)) {
-    return FALSE;
-  }
+function login_destination_apply_redirect() {
+
   $mode = variable_get('ld_condition_type', LOGIN_COND_ALWAYS);
   if ($mode == LOGIN_COND_ALWAYS ) {
     return TRUE;

