ALISO VIEJO, Calif. — While working on a custom registration form in Drupal 6, I came across the need to update user profile fields programmatically from a custom module. In this situation, I wanted to register a user and add values to their profile field all at the same time. In Drupal 6, you have to successfully register the user before you can add values to profile fields. I should say that I am using the Drupal 6 'profile module' to handle profile fields.
In my Drupal 6 custom module I used drupal_execute() to programmatically submit my custom user registration form. When using drupal_execute() to submit the registration form, drupal will login the user and set the $user object.
In my code, I waited to receive a success message from my drupal_execute() function and then loaded the newly populated $user object. With that object, I can retrieve the user id of the newly created user. With the user id and the value of the profile field handy, we can fire profile_save_profile() to populate the profile fields. The value of the profile field needs to be in a properly formatted array.
Joe Ybarra
Web developer and designer
Here is my sample code:
<?php
// I copied and pasted the profile_save_profile() into my custom module and renamed it.
// For the purpose of this blog, my module is called 'mymodule'. I am not sure if this is
// the standard practice for utilizing module functions, but I felt it was safer for me
function mymodule_save_profile($edit, $user, $category, $register = FALSE) {
$result = _profile_get_fields($category, $register);
while ($field = db_fetch_object($result)) {
if (_profile_field_serialize($field->type)) {
$edit[$field->name] = serialize($edit[$field->name]);
}
db_query("DELETE FROM {profile_values} WHERE fid = %d AND uid = %d", $field->fid, $user->uid);
db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $field->fid, $user->uid, $edit[$field->name]);
// Mark field as handled (prevents saving to user->data).
$edit[$field->name] = NULL;
}
}
if (drupal_execute('user_register', $form_state) == '') {
//Registration was successful! Now lets add info to the profile
global $user;
//This will be our Profile field and field value
$foo = 'bar';
//We need to set up the Profile Field Name and Value into an array
$edit = array(
'profile_fieldname'=> $foo
);
// Use our copied function to save the field profile_fieldname with the value of $foo
mymodule_save_profile($edit, $user, 'Profile Category Name'));
}
?>The code is commented but here is a quick run through of what I did. I grabbed the profile_save_profile() from the profile.module file and copied it into my custom module file. I renamed the function mymodule_save_profile(). I dont know if the is best practice, but for my situation, I wanted to have complete control of that function so I renamed it and made it my own. Once I receive the success message that the user is registered I load the $user object and build my array in the way that mymodule_save_module() wants to see it.
Add a Comment