You are here

function salesforce_mapping_related_entity_fieldmap_pull_value in Salesforce Suite 7.3

Pull value callback for property fieldmap type.

1 string reference to 'salesforce_mapping_related_entity_fieldmap_pull_value'
salesforce_mapping_salesforce_mapping_fieldmap_type in modules/salesforce_mapping/includes/salesforce_mapping.fieldmap_type.inc
Implements hook_salesforce_mapping_fieldmap_type().

File

modules/salesforce_mapping/includes/salesforce_mapping.fieldmap_type.inc, line 158
Data and callbacks for fieldmap types.

Code

function salesforce_mapping_related_entity_fieldmap_pull_value($entity_wrapper, $sf_object, $field_map) {
  $mapping_object = salesforce_mapping_object_load_by_sfid($sf_object[$field_map['salesforce_field']['name']]);
  $info = $entity_wrapper
    ->info();
  if ($mapping_object && !isset($info['relation_type'])) {
    $entity = entity_load_single($mapping_object->entity_type, $mapping_object->entity_id);
    return entity_metadata_wrapper($mapping_object->entity_type, $entity);
  }
  elseif (module_exists('relation') && isset($info['relation_type'])) {

    // We cannot create relationships between new items. We are saving them here
    // to avoid performing a duplicate save for all entities in
    // salesforce_pull_process_records().
    if (!$info['parent']
      ->getIdentifier()) {
      $info['parent']
        ->save();
    }
    list($parent_entity_id) = entity_extract_ids($info['parent']
      ->type(), $info['parent']
      ->value());
    $parent_type = $info['parent']
      ->type();
    $query = relation_query($parent_type, $parent_entity_id);
    $query
      ->propertyCondition('relation_type', $info['relation_type']);
    $results = $query
      ->execute();
    if ($results) {
      $relations = relation_load_multiple(array_keys($results));
      $match = TRUE;
    }
    else {
      $match = FALSE;
    }
    if (isset($relations)) {
      foreach ($relations as $rid => $relation) {
        $match = TRUE;

        // Check if matches existing.
        $current_endpoints = $relations[$rid]->endpoints;
        if ($current_endpoints[LANGUAGE_NONE][0]['entity_type'] != $parent_type || $current_endpoints[LANGUAGE_NONE][0]['entity_id'] != $parent_entity_id || $current_endpoints[LANGUAGE_NONE][1]['entity_type'] != $mapping_object->entity_type || $current_endpoints[LANGUAGE_NONE][1]['entity_id'] != $mapping_object->entity_id) {
          $match = FALSE;
        }

        // If same type and not a match delete relation.
        if ($current_endpoints[LANGUAGE_NONE][1]['entity_type'] == $mapping_object->entity_type && !$match) {
          relation_delete($rid);
        }
      }
    }

    // Create new relation and endpoints.
    if ($mapping_object && !$match) {
      $relation_type = $info['relation_type'];
      $endpoints = array(
        array(
          'entity_type' => $parent_type,
          'entity_id' => $parent_entity_id,
        ),
        array(
          'entity_type' => $mapping_object->entity_type,
          'entity_id' => $mapping_object->entity_id,
        ),
      );
      $relation_new = relation_create($relation_type, $endpoints);
      relation_save($relation_new);
    }
  }
  return NULL;
}