public function UserCreate::doExecute in CiviCRM Entity 8.3
File
- src/
Plugin/ RulesAction/ UserCreate.php, line 118
Class
- UserCreate
- Provides a 'User create' action.
Namespace
Drupal\civicrm_entity\Plugin\RulesActionCode
public function doExecute($contact_id, $is_active, $notify, $signin, $format) {
$contact = $this->civicrmApi
->getSingle('Contact', [
'return' => [
'email',
'contact_type',
],
'id' => $contact_id,
]);
if (empty($contact) || empty($contact['email'])) {
return;
}
$params = [
'name' => $format,
'mail' => $contact['email'],
'init' => $contact['email'],
'status' => (bool) $is_active,
];
$this->civicrmApi
->civicrmInitialize();
$config = \CRM_Core_Config::singleton();
if ($this
->checkUserNameExists($params, $config->userSystem)) {
$counter = 0;
do {
$params['name'] = $params['name'] . '_' . $counter++;
} while ($this
->checkUserNameExists($params, $config->userSystem));
}
/** @var \Drupal\user\UserInterface $user */
$user = $this->userStorage
->create($params);
$violations = $user
->validate()
->getByFields(array_keys($params));
if ($violations
->count() > 0) {
$messages = 'Unable to create user for %email due to the following error(s):<ul>';
/** @var \Symfony\Component\Validator\ConstraintViolation $violation */
foreach ($violations as $violation) {
$messages .= '<li>' . $violation
->getMessage() . '</li>';
}
$messages .= '</ul>';
\Drupal::logger('civicrm_entity')
->error($messages, [
'%email' => $params['mail'],
]);
}
if ($violations
->count() === 0 && $user
->save()) {
$this->civicrmApi
->civicrmInitialize();
if ($contact['contact_type'] === 'Individual') {
\CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user
->id(), $contact['email'], 'Drupal8', NULL, 'Individual', TRUE);
}
else {
\CRM_Core_BAO_UFMatch::synchronizeUFMatch($user, $user
->id(), $contact['email'], 'Drupal8', NULL, NULL, TRUE);
}
$this->messenger
->addStatus($this
->t('User with username @name has been created.', [
'@name' => $user
->getUsername(),
]));
$this
->setProvidedValue('civicrm_user', $user);
if ((bool) $signin) {
user_login_finalize($user);
}
if ((bool) $notify) {
_user_mail_notify('register_no_approval_required', $user);
}
}
}