5

I am trying to create a module where the users creates his account and on submit, i get his information and insert them in a second database too. I mean that he will exist in both databases and in Drupals user table and in user table of the other database.

How can i get his information and insert them to a custom database?

I am totally new to Drupal development.

Thank you in advance for any help or advice.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
JcDenton86
  • 933
  • 1
  • 12
  • 32

1 Answers1

6

You will need to implement hook_form_alter() and use the following code:

function [YOUR_MODULE]_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == "user_register_form")
    {
        $form['#submit'][] = "your_custom_submit_callback";
    }
}

Then create the custom submit callback to manipulate the submitted values the way you like:

function your_custom_submit_callback($form, &$form_state)
{
    // your code goes here...
}

Hope this works... Muhammad.

Muhammad Reda
  • 26,379
  • 14
  • 93
  • 105
  • So, inside the custom function (your_custom_submit_callback) i have to get some specific fields (such as username, password etc) and then connect to the custom database and insert the data there too? Am i thinking it right? – JcDenton86 Oct 24 '12 at 11:23
  • Yes, that's right. all form values are inside `$form_state['values']`, you can use `print_r($form_state['values'])` to dig inside. -Muhammad. – Muhammad Reda Oct 24 '12 at 11:54
  • I think and correct me if i am wrong, that the line `$form['submit][]="custom_submit_callback"` doesn't work on Drupal 7. Maybe it's Drupal 6. Because when i comment the line my module works. Is that correct? if not, then why it doesn't work under D7? You helped me a lot to get an idea and get started. – JcDenton86 Oct 24 '12 at 13:06
  • 1
    it's `form[#submit][]`, I've updated the answer. Sorry that I forgot the `#`. Hope it works now. – Muhammad Reda Oct 24 '12 at 13:25
  • Hello again! I managed to do what i wanted with the hook_form_alter function. Now, i am trying to use hook_user_update. So when the user changes some fields in his profile form then i have to update the external database too. So, can you give tips on how to get the fields that changed. I want to take the password, if changed, in plaintext. – JcDenton86 Oct 25 '12 at 11:05
  • you don't need to implement `hook_user_update`, you can still use `hook_form_alter` but this time change the form_id to `user_profile_form` then you can select from the other database where email=email and update the user information. -Muhammad. – Muhammad Reda Oct 25 '12 at 12:04
  • Hi and thanks! This is what i did in the first place but then i came up with hook_user_update because in the $edit i have the new values and in the $account the old ones and i thought that it would be better that way. Maybe then, i will try my first approach with hook_form_alter. – JcDenton86 Oct 25 '12 at 12:11