Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackrabbithanna/b7219b39eaaddd6a46a0f595dd8a5b4c to your computer and use it in GitHub Desktop.
Save jackrabbithanna/b7219b39eaaddd6a46a0f595dd8a5b4c to your computer and use it in GitHub Desktop.
/**
* Implements hook_civicrm_post().
*
* Manage Drupal group membership when Committee Relationship is added/updated/deleted
*
* @param $op
* @param $objectName
* @param $objectId
* @param $objectRef
*/
function committee_collaboration_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$managed_ops = ['create', 'delete', 'edit'];
// hard coded relationship type conditional
if ($objectName == 'Relationship' && in_array($op, $managed_ops) && $objectRef->relationship_type_id == 24) {
if (!empty($objectRef->contact_id_a) && !empty($objectRef->contact_id_b)) {
$civicrm_api = \Drupal::service('civicrm_entity.api');
try {
$organization_contact = $civicrm_api->get('Contact', [
'sequential' => 1,
'id' => $objectRef->contact_id_b,
'return' => ["id", "contact_type", "contact_sub_type", "contact_is_deleted", "custom_169"],
]);
/// hard coded contact type / sub type
// hard coded custom field id, custom field on "committee" contact type, that stores the Drupal group id
if (!empty($organization_contact[0]['contact_type']) &&
$organization_contact[0]['contact_type'] == 'Organization' &&
empty($organization_contact[0]['contact_is_deleted']) &&
!empty($organization_contact[0]['contact_sub_type']) &&
in_array('Committee', $organization_contact[0]['contact_sub_type']) &&
!empty($organization_contact[0]['custom_169'])
) {
$committee_group = \Drupal::entityTypeManager()->getStorage('group')->load($organization_contact[0]['custom_169']);
if (!empty($committee_group)) {
$individual_contact = $civicrm_api->get('Contact', [
'sequential' => 1,
'id' => $objectRef->contact_id_a,
]);
if (!empty($individual_contact[0]['contact_type']) && $individual_contact[0]['contact_type'] == 'Individual' && empty($individual_contact[0]['contact_is_deleted'])) {
$uf_match_results = $civicrm_api->get('UFMatch', [
'sequential' => 1,
'contact_id' => $objectRef->contact_id_a,
]);
if (!empty($uf_match_results[0]['uf_id'])) {
// load user object
$user = User::load($uf_match_results[0]['uf_id']);
// load group membership loader
$group_membership_loader = \Drupal::service('group.membership_loader');
$group_membership = $group_membership_loader->load($committee_group, $user);
switch($op) {
// if relationship is created, add the group membership if one doesn't already exist
case 'create':
if (empty($group_membership)) {
// hard coded group role
$committee_group->addMember($user, ['group_roles' => ['committee-committee_member']]);
}
break;
case 'edit':
// if the relationship is updated, is the status now active? If so, make sure user is a group member
// hardcoded group role
if ($objectRef->is_active && empty($group_membership)) {
$committee_group->addMember($user, ['group_roles' => ['committee-committee_member']]);
}
// if status is now in-active, remove the group membership
if (!$objectRef->is_active && !empty($group_membership)) {
$committee_group->removeMember($user);
}
break;
case 'delete':
// if relationship is deleted, and there was a group membership, remove the group membership
if (!empty($group_membership)) {
$committee_group->removeMember($user);
}
break;
}
}
}
}
}
}
catch (Exception $e) {
\Drupal::logger('committee_collaboration')->error($e->getMessage());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment