保存用户数据后调用user_login_finalize($user),如何修改默认重定向路径?
user_login_finalize($user) Got it, let's break this down based on which Drupal version you're working with—since the approach shifts a bit between Drupal 7 and 8/9/10.
For Drupal 7
Option 1: One-off Fix for Your Programmatic Call
Right before invoking user_login_finalize($user), manually set the destination GET parameter. This overrides the default /node redirect because user_login_finalize relies on drupal_goto(), which prioritizes the destination parameter first:
// Set your custom redirect path (e.g., a user-specific dashboard) $_GET['destination'] = 'user/' . $user->uid . '/dashboard'; // Run the login finalization with your custom redirect user_login_finalize($user);
Option 2: Global Redirect Override (Applies to All Logins)
If you want this redirect to work for every login (including form-based logins), implement the hook_user_login() hook in your custom module:
/** * Implements hook_user_login(). */ function your_custom_module_user_login(&$edit, $account) { // Replace 'custom-path' with your desired redirect URL $edit['redirect'] = 'custom-path'; }
The $edit['redirect'] value will overwrite the default /node target that user_login_finalize uses.
For Drupal 8/9/10
Option 1: Pass the Redirect Directly in the Function Call
Even better—user_login_finalize() accepts an optional second parameter for the redirect! You can pass a Url object directly when calling the function, no hooks needed for one-off scenarios:
// Generate your custom redirect URL (using a route name is best practice) $redirect_url = \Drupal\Core\Url::fromRoute('entity.user.canonical', ['user' => $user->id()]); // Finalize login with your custom redirect user_login_finalize($user, $redirect_url);
Option 2: Global Redirect via Event Subscriber
If you want all logins (form and programmatic) to use your custom redirect, create an event subscriber to hook into the UserLoginEvent:
- Create the subscriber class in your custom module (e.g.,
src/EventSubscriber/UserLoginRedirectSubscriber.php):
namespace Drupal\your_custom_module\EventSubscriber; use Drupal\user\Event\UserLoginEvent; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\RedirectResponse; use Drupal\Core\Url; class UserLoginRedirectSubscriber implements EventSubscriberInterface { /** * {@inheritdoc} */ public static function getSubscribedEvents() { $events[UserLoginEvent::NAME][] = ['onUserLogin', 0]; return $events; } /** * Custom login redirect logic. */ public function onUserLogin(UserLoginEvent $event) { // Replace with your desired route or path $target_url = Url::fromRoute('system.front_page'); $event->setResponse(new RedirectResponse($target_url->toString())); } }
- Register the subscriber in your module's
your_custom_module.services.ymlfile:
services: your_custom_module.user_login_redirect_subscriber: class: Drupal\your_custom_module\EventSubscriber\UserLoginRedirectSubscriber tags: - { name: event_subscriber }
Pick the option that aligns with your specific needs—whether you need a one-time fix or a global change!
内容的提问来源于stack exchange,提问作者Zisu

