Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create new drupal user from webform using webform handler and link to CiviCRM existing contact
<?php
namespace Drupal\my_module\Plugin\WebformHandler;
use Drupal\webform\Plugin\WebformHandlerBase;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\user\Entity\User;
/**
* Create a new user entity from a webform submission.
*
* @WebformHandler(
* id = "create_user",
* label = @Translation("Create User"),
* category = @Translation("Entity creation"),
* description = @Translation("Creates a new drupal user."),
* cardinality =
* \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED,
* results =
* \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED,
* )
*/
class CreateUserHandler extends WebformHandlerBase {
/**
* {@inheritdoc}
*/
public function postSave(WebformSubmissionInterface $webformSubmission, $update = TRUE) {
$user = $this->createUser($webformSubmission);
// Get the civi contact id for the user and check if they already have a drupal account
$civicrm = \Drupal::service('civicrm');
$civicrm->initialize();
// Get Civi contact ID from Drupal user ID
try {
$contact = civicrm_api3('Contact', 'get', [
'sequential' => 1,
'return' => ["id"],
'contact_type' => "",
'email' => $user->getEmail(),
]);
$cid = $contact['values'][0]['contact_id'];
//create record in ufmatch to link contact to Drupal record
}
catch (CiviCRM_API3_Exception $e) {
$error = $e->getMessage();
}
// Link contact to Drupal account
/** @var \Drupal\Core\Database\Connection $connection */
$connection = \Drupal::service('database');
$result = $connection->insert('civicrm_uf_match')
->fields([
'uf_id' => $user->id(),
'domain_id' => 1,
'uf_name' => $user->getAccountName(),
'contact_id' => $cid,
])
->execute();
}
/**
* Create user.
*
* @param WebformSubmissionInterface $webformSubmission
*
* @return User
*/
protected function createUser(WebformSubmissionInterface $webformSubmission): User {
$values = $webformSubmission->getData();
$email = $values['civicrm_1_contact_1_email_email'];
$name = mb_substr($values['civicrm_1_contact_1_contact_first_name'], 0, 1, "UTF-8") . '.' . $values['civicrm_1_contact_1_contact_last_name'];
$name = $this->check_name_exists($name);
// This needs to go in a validation function for the webform
//if (user_load_by_mail($email) !== FALSE) {
// return "I'm sorry, an account with that email already exists";
//}
/** @var User $user */
$user = User::create();
// mandatory fields
//$user->setPassword($webformSubmission->getData('password'));
$user->setPassword(user_password());
$user->setEmail($email);
$user->setUsername($name);
$user->addRole('applicant');
$user->enforceIsNew();
// optional fields
$user->set('init', $email);
$user->set('langcode', 'en');
$user->set('preferred_langcode', 'en');
$user->set('preferred_admin_langcode', 'en');
$user->activate();
$user->save();
_user_mail_notify('register_no_approval_required', $user);
//_user_mail_notify('register_admin_created',$user);
return $user;
}
protected function check_name_exists($name) {
if (user_load_by_name($name) !== FALSE) {
//if name exists add a random number to the name between 1 and 99 - should work for most cases
$name = $name . '_' . rand(1,99);
$this->check_name_exists($name);
}
return $name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment