You are here

crm_core_user_sync.module in CRM Core 8.3

CRM Core User Synchronization module.

File

modules/crm_core_user_sync/crm_core_user_sync.module
View 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.

  /** @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation $service */
  $service = \Drupal::service('crm_core_user_sync.relation');
  $related_contact_id = $service
    ->getIndividualIdFromUserId($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) {
      $service
        ->relate($account);
    }
  }
}

/**
 * Implements hook_user_delete().
 */
function crm_core_user_sync_user_delete(UserInterface $account) {

  /** @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation $service */
  $service = \Drupal::service('crm_core_user_sync.relation');
  $relation_id = $service
    ->getRelationIdFromUserId($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) {

  /** @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation $service */
  $service = \Drupal::service('crm_core_user_sync.relation');
  $relation_id = $service
    ->getRelationIdFromIndividualId($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')) {

      /** @var \Drupal\crm_core_user_sync\CrmCoreUserSyncRelation $service */
      $service = \Drupal::service('crm_core_user_sync.relation');
      $individual_id = $service
        ->getIndividualIdFromUserId($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 User Sync Relation');
  $data['users_field_data']['crm_core_user_sync_relation'] = [
    'title' => $title,
    'help' => t('Adds relation to CRM 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 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

Namesort descending 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().