I started with a fresh install of Drupal 8 and did a migration of about 5K users. I am trying to force a recreation of the UF Matches. I cannot find a way to get CiviCRM to do this so I wrote the following code but this is not working either. Currently it delete the UFMatch from the table but it does not finish the final API call that creates the new match.

$q = \Drupal::entityQuery('user');
$q->accessCheck(FALSE); //This is ran under cron
$q->sort('created', 'DESC');
$users = $q->execute();
foreach ($users as $uid) {
    $drupal_user = \Drupal\user\Entity\User::load($uid);
    $user_email = $drupal_user->getEmail();
    $result = civicrm_api3('UFMatch', 'get', [
        'sequential' => 1,
        'return' => ["contact_id", "uf_id"],
        'uf_name' => $user_email,
    print $user_email;
    if ($result['is_error'] == 0) {
        foreach ($result['values'] as $value) {
            $uf_id = $value['uf_id'];
            $contact_id = $value['contact_id'];
            $uf_match_table_id_result_array = civicrm_api3('UFMatch', 'get', [
                'sequential' => 1,
                'return' => ["id"],
                'contact_id' => $contact_id,
            $uf_match_table_id = $uf_match_table_id_result['id'];
            if ($uf_match_table_id_result_array['is_error'] == 0) {
                foreach ($uf_match_table_id_result_array['values'] as $uf_match_table_id_result) {
                    $delete = civicrm_api3('UFMatch', 'delete', [
                        'id' => $uf_match_table_id,

                    if ($result['is_error'] == 0) {
                        $create_match = civicrm_api3('UFMatch', 'create', [
                            'uf_id' => $uf_id,
                            'uf_name' => $user_email,
                            'contact_id' => $contact_id,

I think the issue might be the $uf_id variable that I am getting from the contact and using in the UFMatch create API call.

Any ideas how to do this better or help with the code is greatly appreciated.

Thanks, Steve

  • 1
    did you a/ empty your uf_match table, b/ use the synch users option which should in theory then match a drupal user to the first civi ID with matching email – petednz - fuzion Jul 13 at 8:44

Did you try 'Synchronize Users-to-Contacts' from UI? You need to make sure you have unsupervised dedupe rule for individual set to email only before running 'Synchronize Users-to-Contacts'

| improve this answer | |
  • I forgot that this existed and when I Googled for it, somehow I did not find it. Unfortunately it is bidirectional so it is creating junk CiviCRM accounts for users who should be deleted but do not match members in CiviCRM. Easy enough to clean up with a search after the synchronization but I am going to see if I can deconstruct the code and make it run as a script just from the existing emails (in CiviCRM) to the users. – Josh Jul 13 at 18:14
  • I dont think it creates user account for civi contact. It maps cms user with civi contact based on email address. So you need to make sure you have unsupervised rule set to email only so that it doesn't create duplicates – Pradeep Nayak Jul 13 at 18:17
  • HI Pradeep Nayak, I have played with it a lot and the problem is that it creates CiviCRM account for every Drupal user. This does not match our use case. Ideally we would want to go the other way around and find CiviCRM Contacts that match users and then make the match like I was trying to do above. I think my problem is with my API call to add the ufmatch record. Thanks! – Josh Jul 20 at 22:45
  • What is your unsupervised Individual rule? – Pradeep Nayak Jul 20 at 23:21
  • sorry it has taken a while to get back to you. It is email only. You can see onlinesharing18.s3.us-west-2.amazonaws.com/civicrm/… Thank you! – Josh Jul 26 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.