Use the Silent User Registration extension https://lab.civicrm.org/extensions/silentuserreg This will create the user in Drupal
Before you install the extension, replace the Createcmsuser.php file in CRM > Silentuserreg > CivirulesAction with the code below. This will create the UF Match that you are looking for to link it to the user in Civi
Follow the extension instructions to set it up. I would have your CiviRule trigger off When contact is added to group or something like that.
<?php
/**
- Carls Notes
- This file is to create a CRMUser and then a UFMatch record. File Location=secure/sites/all/files/civicrm/extensionsions/silentuserreg-master/CRM/Silentuserreg/Silentuserreg
/
/*
- Class for CiviRules Create User Action
- @author Jon Goldberg ([email protected])
- @license AGPL-3.0
*/
class CRM_Silentuserreg_CivirulesAction_Createcmsuser extends CRM_Civirules_Action {
/**
- Method processAction to execute the action
- @param CRM_Civirules_TriggerData_TriggerData $triggerData
- @access public
/
public function processAction(CRM_Civirules_TriggerData_TriggerData $triggerData) {
$cid = $triggerData->getContactId();
$userInfo = \Civi\Api4\Email::get()
->addSelect('id', 'email', 'contact.display_name', 'is_primary')
->addWhere('is_primary', '=', TRUE)
->addWhere('contact_id', '=', $cid)
->setCheckPermissions(FALSE)
->execute()
->first();
$userExists = (bool) count(\Civi\Api4\UFMatch::get()
->addWhere('contact_id', '=', $cid)
->setCheckPermissions(FALSE)
->execute());
if (!$userExists) {
$userParams['cms_name'] = $userParams['nickname'] = $userInfo['contact.display_name'];
$userParams['email'] = $userInfo['email'];
$userParams['cms_pass'] = CRM_Silentuserreg_Util::randomPassword();
$nid=CRM_Core_BAO_CMSUser::create($userParams, 'email');
// create the UF Match record
$ufmatch['uf_id'] = $nid;
$ufmatch['contact_id'] = $cid;
$ufmatch['uf_name'] = $userInfo['email'];
CRM_Core_BAO_UFMatch::create($ufmatch);
}
}
/*
- Method to return the url for additional form processing for action
- and return false if none is needed
- @param int $ruleActionId
- @return bool
- @access public
*/
public function getExtraDataInputUrl($ruleActionId) {
return FALSE;
}
}