0

Have a requirement to develop drupal 9 custom module to get the mail id from the user registration form and store it separate table. I tried the hook_user_presave function to get the mail id but was not able to get it. Hereby sharing the code

function save_mail_user_presave(UserInterface $user)
 {

     try{
                $conn = Database::getConnection();
                $fields["mail"] = $user->get('mail')->value;
            $conn->insert('save_email')
                           ->fields($fields)->execute();
                  \Drupal::messenger()->addMessage($this->t(' Thank you !! Your>

        } catch(Exception $ex){
                \Drupal::logger('save_email')->error($ex->getMessage());
        }

}

Anybody helps me figure out the problem

4
  • The question should explain what was not able to get it means. Do you mean that, with the code you are using, $fields["mail"] doesn't contain the user email? Do you get any error message? Which one?
    – apaderno
    Commented Feb 5, 2022 at 16:18
  • As side note, there are two errors in the shown code: $this cannot be used outside a class method, for example inside a function; the literal string passed to $this->t() doesn't have the last string delimiter. Verify the code you are using is correct PHP code and be sure you correctly copied the code in the question.
    – apaderno
    Commented Feb 5, 2022 at 16:31
  • Yes not able to catch the email address on .$fields["mail "] Empty. Not getting any error.Thanks
    – srini
    Commented Feb 5, 2022 at 16:45
  • See my second comment. If you are really using that code, it won't work.
    – apaderno
    Commented Feb 5, 2022 at 16:51

1 Answer 1

0

Drupal 9 already stores the email address upon registration in the init column of the users_field_data table. This is the "email address from the user registration form"; it is different from the user's account email address, which is stored in the mail column of the same table.

So, there's no need for a custom module for this; you can use the column that Drupal already provides.

3
  • Thanks Patrick, I want catch the email address even at the invalid form values. Can u suggest way to address this
    – srini
    Commented Feb 5, 2022 at 16:21
  • @srini In that case, you cannot use hook_ENTITY_TYPE_presave() as that hook is invoked when the submitted values passed validation. It's not clear why you would store the email address when the form doesn't pass validation. In that case, you would end up storing multiple emails for a single user, even an empty string.
    – apaderno
    Commented Feb 5, 2022 at 16:39
  • Want to capture for same analysis purpose. Is there any way to capture that. If presave function not works means Please suggest
    – srini
    Commented Feb 5, 2022 at 16:54

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.