public function CrmCoreUserSyncRelation::relate in CRM Core 8.3
Same name and namespace in other branches
- 8 modules/crm_core_user_sync/src/CrmCoreUserSyncRelation.php \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation::relate()
Synchronizes user and contact.
Parameters
\Drupal\user\UserInterface $account: Account to be synchronized. Programmatically created accounts can override default behavior by setting $account->crm_core_no_auto_sync = TRUE.
\Drupal\crm_core_contact\IndividualInterface $individual: Contact to be associated with $account.
Return value
\Drupal\crm_core_contact\ContactInterface A contact object.
Throws
\Drupal\Core\Entity\EntityStorageException
Overrides CrmCoreUserSyncRelationInterface::relate
File
- modules/
crm_core_user_sync/ src/ CrmCoreUserSyncRelation.php, line 162
Class
- CrmCoreUserSyncRelation
- Relation service.
Namespace
Drupal\crm_core_user_syncCode
public function relate(UserInterface $account, IndividualInterface $individual = NULL) {
// No contact and $account->crm_core_no_auto_sync => no sync.
if (empty($individual) && !empty($account->crm_core_no_auto_sync)) {
return NULL;
}
if (empty($individual)) {
if ($this
->getIndividualIdFromUserId($account
->id())) {
// Account already has related contact.
return NULL;
}
$contact_type = $this->rules
->getContactType($account);
if (!$contact_type) {
// No rules configured on this type.
return NULL;
}
/** @var \Drupal\crm_core_contact\Entity\IndividualType $type */
$type = $this->entityTypeManager
->getStorage('crm_core_individual_type')
->load($contact_type);
$fields = $type
->getPrimaryFields();
// TODO: Inject.
$config = \Drupal::config('crm_core_user_sync.settings');
if ($config
->get('auto_sync_user_relate') && isset($fields['email']) && !empty($fields['email'])) {
$matches = $this->individualStorage
->loadByProperties([
$fields['email'] => $account
->getEmail(),
'type' => $contact_type,
]);
if (count($matches) === 1) {
$individual = reset($matches);
}
}
if (empty($individual)) {
/** @var \Drupal\crm_core_contact\Entity\Individual $individual */
$individual = $this->individualStorage
->create([
'type' => $contact_type,
]);
$individual
->setOwnerId($this->currentUser
->id());
// For now we just add the name.
$individual->name->given = $account
->getAccountName();
if (isset($fields['email']) && !empty($fields['email'])) {
$individual
->set($fields['email'], $account
->getEmail());
}
$individual
->save();
}
}
// Check if contact can be synchronized to a contact.
if (!$this->rules
->valid($account, $individual)) {
return NULL;
}
// Check if crm_core_user_sync relation exists for any of endpoint.
if ($this
->getIndividualIdFromUserId($account
->id()) || $this
->getUserIdFromIndividualId($individual
->id())) {
return NULL;
}
$relation = Relation::create();
$relation
->setUser($account);
$relation
->setIndividual($individual);
$relation
->save();
$this->logger
->notice('User @user @uid has been synchronized to the contact @contact_id, relation @rid has been created.', [
'@user' => $account
->getDisplayName(),
'@uid' => $account
->id(),
'@contact_id' => $individual
->id(),
'@rid' => $relation
->id(),
]);
return $individual;
}