I have a custom module that will create a Drupal user programmatically using user_save
as follows:
//set up the user fields
$fields = array(
'mail' => $email,
'pass' => $password,
'name' => $username,
'status' => 1,
'init' => $email,
'access' => REQUEST_TIME,
'roles' => array(2 => 'authenticated user'),
);
//the first parameter is left blank so a new user is created
$account = user_save('', $fields);
I have first done a check using the CiviCRM API to make sure that if an existing contact is already in the database and has a Drupal account associated with it, we don't create a new one. Also I check to make sure the email address isn't registered from the Drupal side.
The problem is, when I create a new user, it is not linking up to an existing CiviCRM contact record that uses the same email address and I'm ending up with a duplicate contact record.
How can I force the site to link to an existing record if the email address is a match?