Make sure you have the Rules module installed.
Create a new triggered rule that occurs when a new user is created.
Create a new action for this rule that executes custom php.
I added this code as my custom php (omit php tags):
profile_load_profile($account);
$firstname = $account->profile_first_name;
$lastname = $account->profile_last_name;
$displayname = $account->name;
$email = $account->mail;
$street = $account->profile_street;
$city = $account->profile_city;
$state = $account->profile_state;
$postalcode = $account->profile_postal_code;
$country = $account->profile_country;
$fax = $account->profile_fax_number;
$phone = $account->profile_phone_number;
$mobile = $account->profile_mobile;
db_set_active('contacts');
$result = db_query("SELECT contact_id FROM civicrm_email WHERE email = '$email'");
while ($node = db_fetch_object($result)) {
$userId = $node->id;
}//end while statement
//insert names into civicrm
//db_query("SELECT * FROM civicrm_contact WHERE id = $userId");
db_query("UPDATE civicrm_contact SET display_name = '$displayname' WHERE id = $userId");
db_query("UPDATE civicrm_contact SET first_name = '$firstname' WHERE id = $userId");
db_query("UPDATE civicrm_contact SET last_name = '$lastname' WHERE id = $userId");
//insert addresses into civicrm
if ($street != '' || $street != NULL || $street != 0 || $city != '' || $city != NULL || $city != 0)
{
db_query("INSERT INTO civicrm_address (street_address, city, contact_id) values ('$street', '$city', $userId)");
}
db_set_active('default');
This code selects the database that stores the civicrm data, loads the profile fields from drupal and writes to the database that stores the civicrm data.
For this code snippet to work you'll need to modify your Drupal settings file so that it allows you to select an alternative database.
Eg.
$db_url['default'] = 'mysqli://user:pass@localhost/drupaldb';
$db_url['contacts'] = 'mysqli://user:pass@localhost/civicrmdb';
Comments
work around for Gigya
PLEASE NOTE: this is a bit of a hack, so any advice on how to clean it up would be greatly appreciated.
I was trying to do this same thing, except I was also using the Gigya module for authentication. the above didn't work, because the Gigya authentication doesn't happen immediately upon creating a new account, so the above was registering an anonymous user.
So what I did is set up a rule with action 'upon viewing a user profile' (which is where the user is redirected on login) with the following two conditions:
and
and then the rule was: