You are here

function _crm_core_user_sync_get_related_entity 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_entity()

Copied from relation.module, (beta 1) gives an error if the entity id is not found, so need to fix that

4 calls to _crm_core_user_sync_get_related_entity()
crm_core_user_sync_contact_get_user_properties in modules/crm_core_user_sync/crm_core_user_sync.module
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.
crm_core_user_sync_user_update in modules/crm_core_user_sync/crm_core_user_sync.module
Implements hook_user_update()
views_plugin_argument_default_contact_id::get_argument in modules/crm_core_user_sync/views/views_plugin_argument_default_contact_id.inc

File

modules/crm_core_user_sync/crm_core_user_sync.module, line 354

Code

function _crm_core_user_sync_get_related_entity($entity_type, $entity_id, $relation_type = NULL, $r_index = NULL) {
  $query = relation_query($entity_type, $entity_id);
  if ($relation_type) {
    $query
      ->propertyCondition('relation_type', $relation_type);
  }
  if (isset($r_index)) {

    // $query->propertyCondition('r_index', $r_index);
    $query
      ->fieldCondition('endpoints', 'r_index', $r_index);
  }
  $results = $query
    ->execute();
  if (empty($results)) {
    return;
  }
  $result = reset($results);
  $relation = Relation::load($result->relation_id);
  $request_key = $entity_type . ':' . $entity_id;
  $entities = $relation->endpoints[LANGUAGE_NONE];
  if (isset($entities[0]['entity_type']) && isset($entities[0]['entity_id'])) {
    $first_entity_key = $entities[0]['entity_type'] . ':' . $entities[0]['entity_id'];
    if (isset($r_index)) {
      $request_key = $request_key . ':' . $r_index;
      $first_entity_key = $first_entity_key . ':' . $entities[0]['r_index'];
    }
    if ($request_key == $first_entity_key) {
      $other_endpoints = entity_load($entities[1]['entity_type'], array(
        $entities[1]['entity_id'],
      ));
      return reset($other_endpoints);
    }
    $other_endpoints = entity_load($entities[0]['entity_type'], array(
      $entities[0]['entity_id'],
    ));
    return reset($other_endpoints);
  }
}