Last updated 4 September 2009. Created on 2 June 2009.
Edited by bekasu, scarer. Log in to edit this page.

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):

$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;


$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)");


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.


$db_url['default'] = 'mysqli://user:pass@localhost/drupaldb';
$db_url['contacts'] = 'mysqli://user:pass@localhost/civicrmdb';

Looking for support? Visit the forums, or join #drupal-support in IRC.


8ballsaysiwin’s picture

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:

$firstname = $account->profile_first_name;
global $user;
$uid = $user->uid;
$civiid = db_result(db_query("SELECT contact_id FROM {civicrm_uf_match} WHERE uf_id = %d", $uid));
$result = db_result(db_query("SELECT first_name FROM {civicrm_contact} WHERE id = %d", $civiid));
return $result != $firstname;


return $account->uid == $user->uid;

and then the rule was:

drupal_set_message("This is your account page. Click Edit to change your password or account info. Click My Friends to see which of your current Social Networking Friends are on this site.");
// set all of our variables

$firstname = $account->profile_first_name;
$lastname = $account->profile_last_name;
$displayname = $firstname;
$displayname .= " ";
$displayname .= $lastname;
$firstname = $account->profile_first_name;
$lastname = $account->profile_last_name;
$sortname = $lastname;
$sortname .= ", ";
$sortname .= $firstname;
$email = $account->mail;
$street = $account->profile_street_address;
$city = $account->profile_city;
$state = $account->profile_state;
$postalcode = $account->profile_postal_code;
$county = $account->profile_county;
$mobile = $account->profile_mobile_phone;

// get this new user's uid
global $user;
$uid = $user->uid;

// set the civicrm database as the active one for all queries

// get info about current CiviCRM user id
$civiid = db_result(db_query("SELECT contact_id FROM {civicrm_uf_match} WHERE uf_id = %d", $uid));

//insert the users name into the civicrm database
db_query("UPDATE {civicrm_contact} SET display_name='$displayname', first_name='$firstname', last_name='$lastname', sort_name='$sortname' WHERE id = $civiid");

// insert the users mobile number in the civicrm database
db_query("INSERT INTO {civicrm_phone} (contact_id, phone, phone_type_id, is_primary) VALUES (%d, '%s', %d, %b)", $civiid, $mobile, 2, 1);

// insert the users postal code, since it is a required field
db_query("INSERT INTO {civicrm_address} (postal_code, contact_id) VALUES ('%s', %d)", $postalcode, $civiid);

// get state ID
$stateCode = db_result(db_query("SELECT id FROM {civicrm_state_province} WHERE abbreviation = $state"));

//insert the users addresses into the civicrm db, but first check to make sure they entered at least something in those non-required fields
if ($street != '' || $street != NULL || $street != 0 || $city != '' || $city != NULL || $city != 0 || $state != '' || $state != NULL || $state != 0) {
  db_query("UPDATE {civicrm_address} SET street_address='%s', city='%s', state_province_id=%d, is_primary=1 WHERE contact_id=%d", $street, $city, $stateCode, $civiid);

// insert the users county into the county db fields (mine are custom) (another required field)
db_query("INSERT INTO {civicrm_value_pledge_results_2} (entity_id, user_county_19) VALUES (%d, '%s')", $civiid, $county);

// set db back to drupal db for rest of page load