Score:0

store user entered mail id to separate table

de flag

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

apaderno avatar
us flag
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 avatar
us flag
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.
de flag
Yes not able to catch the email address on .$fields["mail "] Empty. Not getting any error.Thanks
apaderno avatar
us flag
See my second comment. If you are really using that code, it won't work.
Score:0
cn flag

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.

de flag
Thanks Patrick, I want catch the email address even at the invalid form values. Can u suggest way to address this
apaderno avatar
us flag
@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.
de flag
Want to capture for same analysis purpose. Is there any way to capture that. If presave function not works means Please suggest
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.