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.
\Drupal::service('civicrm')->initialize();
$q = \Drupal::entityQuery('user');
$q->accessCheck(FALSE); //This is ran under cron
$q->sort('created', 'DESC');
$q->range(0,1);//Testing
$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