views:

132

answers:

1

I can add an extra field to the registration. What I need to know is what step do I need to take to then grab that input and insert it into the user table of drupal. The code below is in my module this adds just a field to the form, but when its submitted it doesnt do anything with the data.

function perscriptions_user($op, &$edit, &$account, $category = NULL){
  if ($op == 'register') {
    $form['surgery_address'] = array (
      '#type' => 'textarea',
      '#title' => t('Surgery Address'),
      '#required' => TRUE,
    );      

     return $form;     
  }

  if ($op == 'update') {
    // …
  }
}
+5  A: 

As reported in hook_user() documentation:

$op What kind of action is being performed. Possible values (in alphabetical order):
- "insert": The user account is being added. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.
- "update": The user account is being changed. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.
- "validate": The user account is about to be modified. The module should validate its custom additions to the user object, registering errors as necessary.

The module needs to create its own database table in hook_install().

hook_user() could be implemented with the following code, in example:

function perscriptions_user($op, &$edit, &$account, $category = NULL){
  if ($op == 'register' || ($op == 'form' && $category = 'account')) {
    $form['surgery_address'] = array (
      '#type' => 'textarea',
      '#title' => t('Surgery Address'),
      '#required' => TRUE,
    );

    return $form;     
  }

  if ($op == 'insert' || $op == 'update') {
    prescriptions_save_user_profile($account->uid, $edit['surgery_address']);
  }
  if ($op == 'validate' && $category == 'account') {
    // Verify the entered values are valid.
    // In this example, the value is contained in $edit['surgery_address'].
  }
}

prescriptions_save_user_profile() is the function that saves the user profile values in the database. The code checks the category to avoid to show the same form fields in all the tabs shown in the user profile edit form.

kiamlaluno
So are you saying that because I have defined that form field it will add it to the database automatically if it exists. I know how to use the schema api and hook_install(). Am I way off track here? Any more clarification? I just find it hard to understand what the documentation is stating.
Jonathan
@Jonathan: No. It's the module that needs to save the value of the fields it added in the user form. Drupal core code cannot save the fields added from a third-party module in a database table. Actually, Drupal core code will save the extra fields in `$user->data`; that is the reason the documentation suggests "set the saved fields to NULL in `$edit`".
kiamlaluno
@kiamlaluno Right, im understanding more im so close! Could I be cheecky and steal just a couple more minutes of your time to show me how you access the data from the form in relation to my mini example? I can do the adding to the database and the schema myself I just need to figure out at what stage I need to take the data adn how to acess the values. I promise I will give you top marks! :-)
Jonathan
BOOM! excellent. Thank you for your time, code speaks louder than words! Youv'e helped me understand that little extra more! Thank you very much.
Jonathan
I also added the part about validating the form fields, just in case you need it. I have not written code in that case, but you should report any errors using `form_set_error()` (http://api.drupal.org/api/function/form_set_error).
kiamlaluno