crm_core_user_sync.module in CRM Core 8
Same filename and directory in other branches
CRM Core User Synchronization module.
File
modules/crm_core_user_sync/crm_core_user_sync.moduleView source
<?php
/**
* @file
* CRM Core User Synchronization module.
*/
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\crm_core_contact\Entity\Individual;
use Drupal\crm_core_contact\IndividualInterface;
use Drupal\crm_core_user_sync\Entity\Relation;
use Drupal\user\UserInterface;
/**
* Implements hook_user_insert().
*/
function crm_core_user_sync_user_insert(UserInterface $account) {
$auto_sync_user_create = \Drupal::config('crm_core_user_sync.settings')
->get('auto_sync_user_create');
if ($auto_sync_user_create) {
\Drupal::service('crm_core_user_sync.relation')
->relate($account);
}
}
/**
* Implements hook_user_update().
*/
function crm_core_user_sync_user_update(UserInterface $account) {
// User update only ensures that for the given user account there is a
// corresponding contact record.
$related_contact_id = \Drupal::service('crm_core_user_sync.relation')
->getUserIndividualId($account
->id());
if (empty($related_contact_id)) {
$auto_sync_user_create = \Drupal::config('crm_core_user_sync.settings')
->get('auto_sync_user_create');
if ($auto_sync_user_create) {
\Drupal::service('crm_core_user_sync.relation')
->relate($account);
}
}
}
/**
* Implements hook_user_delete().
*/
function crm_core_user_sync_user_delete(UserInterface $account) {
$relation_id = \Drupal::service('crm_core_user_sync.relation')
->getUserRelationId($account
->id());
if ($relation_id) {
$relation = Relation::load($relation_id);
$relation
->delete();
}
}
/**
* Implements hook_crm_core_individual_delete().
*/
function crm_core_user_sync_crm_core_individual_delete(IndividualInterface $individual) {
$relation_id = \Drupal::service('crm_core_user_sync.relation')
->getIndividualRelationId($individual
->id());
if ($relation_id) {
$relation = Relation::load($relation_id);
$relation
->delete();
}
}
/**
* Implements hook_entity_extra_field_info().
*/
function crm_core_user_sync_entity_extra_field_info() {
$contact_show = \Drupal::config('crm_core_user_sync.settings')
->get('contact_show');
if ($contact_show) {
$fields['user']['user']['display']['contact_information'] = [
'label' => t('Contact information'),
'description' => t('Display related contact information'),
'weight' => 0,
];
return $fields;
}
}
/**
* Implements hook_user_view().
*/
function crm_core_user_sync_user_view(array &$build, UserInterface $account, EntityViewDisplayInterface $display) {
$contact_show = \Drupal::config('crm_core_user_sync.settings')
->get('contact_show');
if ($contact_show) {
if ($display
->getComponent('contact_information')) {
$individual_id = \Drupal::service('crm_core_user_sync.relation')
->getUserIndividualId($account
->id());
if ($individual_id) {
$individual = Individual::load($individual_id);
$build['contact_information'] = [
'#type' => 'item',
'#title' => t('Contact Information'),
'#markup' => '<h4 class="label">' . t('Contact name') . '</h4> ' . $individual
->label(),
];
}
}
}
}
/**
* Implements hook_views_data_alter().
*
* @todo Remove once https://www.drupal.org/project/drupal/issues/2706431 is
* resolved.
*/
function crm_core_user_sync_views_data_alter(array &$data) {
$title = t('CRM Core User Sync Relation');
$data['users_field_data']['crm_core_user_sync_relation'] = [
'title' => $title,
'help' => t('Adds relation to CRM Core User Sync Relation that point to current user.'),
'relationship' => [
'base' => 'crm_core_user_sync_relation',
'base field' => 'user_id',
'field' => 'uid',
'id' => 'standard',
'label' => $title,
],
];
$data['crm_core_individual']['crm_core_user_sync_relation'] = [
'title' => $title,
'help' => t('Adds relation to CRM Core User Sync Relation that point to current individual.'),
'relationship' => [
'base' => 'crm_core_user_sync_relation',
'base field' => 'individual_id',
'field' => 'individual_id',
'id' => 'standard',
'label' => $title,
],
];
}
Functions
Name | Description |
---|---|
crm_core_user_sync_crm_core_individual_delete | Implements hook_crm_core_individual_delete(). |
crm_core_user_sync_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
crm_core_user_sync_user_delete | Implements hook_user_delete(). |
crm_core_user_sync_user_insert | Implements hook_user_insert(). |
crm_core_user_sync_user_update | Implements hook_user_update(). |
crm_core_user_sync_user_view | Implements hook_user_view(). |
crm_core_user_sync_views_data_alter | Implements hook_views_data_alter(). |