Drupal-8-user-register-hook [ PROVEN — Collection ]

use Drupal\user\UserInterface; /** * Implements hook_ENTITY_TYPE_presave() for user entities. */ function my_module_user_presave(UserInterface $user) { // Check if this is a new user registration if ($user->isNew()) { // Perform custom logic, e.g., set a field value $user->set('field_welcome_status', 'Pending'); } } Use code with caution. Copied to clipboard 🎯 Key Considerations

To hook into the user registration process in Drupal 8 (and 9/10+), you typically use entity hooks since users are treated as content entities. 🛠️ Recommended Hooks

The most effective way to intercept or modify user data during registration is through the following hooks: Usage Note Runs before the user is saved. drupal-8-user-register-hook

Do you need help on the registration form, or are you looking to redirect users after they sign up?

For cleaner, decoupled code, consider Symfony Event Subscribers if you are using the Hook Event Dispatcher module. 🛠️ Recommended Hooks The most effective way to

Inside presave , always call $user->isNew() to ensure your code only runs during initial registration rather than every time a profile is updated.

Best for modifying data (e.g., adding a default role) before it hits the database. Runs after the user is created. Inside presave , always call $user->isNew() to ensure

This is the standard approach to identify a vs. an existing user being updated.