crm_core_user_sync.module 4.01 KB
Newer Older
1
<?php
2 3

/**
4 5
 * @file
 * CRM Core User Synchronization module.
6
 */
7

8 9
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\crm_core_contact\Entity\Individual;
10
use Drupal\crm_core_contact\IndividualInterface;
11 12
use Drupal\crm_core_user_sync\Entity\Relation;
use Drupal\user\UserInterface;
13

14
/**
15
 * Implements hook_user_insert().
16
 */
17 18 19 20
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);
21 22 23
  }
}

24
/**
25
 * Implements hook_user_update().
26
 */
27 28 29 30 31 32 33 34
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);
ygerasimov's avatar
ygerasimov committed
35
    }
36 37
  }
}
38

39
/**
40
 * Implements hook_user_delete().
41
 */
42 43 44 45 46
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();
47 48 49
  }
}

50 51 52 53 54 55 56 57 58 59 60
/**
 * 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();
  }
}

61
/**
62
 * Implements hook_entity_extra_field_info().
63
 */
64 65 66 67 68 69 70 71
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,
    ];
72

73
    return $fields;
74 75
  }
}
76

77 78 79
/**
 * Implements hook_user_view().
 */
80 81 82 83 84 85 86 87 88 89 90 91 92
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(),
        ];
      }
93
    }
94 95
  }
}
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

/**
 * 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,
    ],
  ];
}