You are here

function _crm_core_user_sync_get_related_entities in CRM Core 8.2

Same name and namespace in other branches
  1. 7 modules/crm_core_user_sync/crm_core_user_sync.module \_crm_core_user_sync_get_related_entities()

Loosely based on relation_rules_get_related_entities.

1 call to _crm_core_user_sync_get_related_entities()
crm_core_user_sync_get_related_entity in modules/crm_core_user_sync/crm_core_user_sync.module
Retrieves the related entity based on entity type, ids and relation type.

File

modules/crm_core_user_sync/crm_core_user_sync.module, line 320

Code

function _crm_core_user_sync_get_related_entities($entity_type, $entity_id, $relation_type) {
  $rids = array_keys(relation_query($entity_type, $entity_id)
    ->entityCondition('bundle', $relation_type)
    ->execute());
  $entities_ids = array();
  if (!$rids) {
    return $entities_ids;
  }
  $target_entity_type = $entity_type == 'user' ? 'crm_core_contact' : 'user';
  $rmap = array();
  foreach (Relation::loadMultiple($rids) as $relation) {
    $data = array();
    foreach ($relation->endpoints[LANGUAGE_NONE] as $endpoint) {
      $data[$endpoint['entity_type']] = $endpoint['entity_id'];
    }
    $entities_ids[] = $data[$target_entity_type];
    $rmap[$data[$target_entity_type]] = $data[$entity_type];
  }
  $entities = entity_load($target_entity_type, $entities_ids);
  $targets = array();
  foreach ($entities as $entity_id => $entity) {
    $targets[$rmap[$entity_id]] = $entity;
  }
  return $targets;
}