You are here

function salesforce_mapping_relation_endpoints_fieldmap_pull_value in Salesforce Suite 7.3

Pull value callback for relation endpoint fieldmap type.

Parameters

EntityMetadataWrapper $entity_wrapper: Wrapper for the related entity being synced.

SalesforceMappingObject $sf_object: Mapped Drupal and Salesforce objects.

array $field_map: Field mapping data.

Return value

array Array of Drupal entities.

1 string reference to 'salesforce_mapping_relation_endpoints_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 260
Data and callbacks for fieldmap types.

Code

function salesforce_mapping_relation_endpoints_fieldmap_pull_value($entity_wrapper, $sf_object, $field_map) {
  $values = array();

  // Load each related Drupal entity.
  foreach ($field_map['salesforce_field'] as $sf_field) {
    $mapping_object = salesforce_mapping_object_load_by_sfid($sf_object[$sf_field['name']]);
    if ($mapping_object) {
      $entity = entity_load_single($mapping_object->entity_type, $mapping_object->entity_id);
      $values[] = entity_metadata_wrapper($mapping_object->entity_type, $entity);
    }
  }

  // Load the relation type entity.
  $relation_type_name = $entity_wrapper
    ->info();
  $relation_type = relation_type_load($relation_type_name['parent']
    ->getBundle());

  // If the relationship is directional, ensure source/bundle are in the right
  // order.
  $return_values = array();
  if ($relation_type->directional) {
    foreach ($values as $value) {
      foreach ($relation_type->source_bundles as $bundle_key) {
        list($source_entity_type) = explode(':', $bundle_key);
        if ($source_entity_type == $value
          ->type()) {
          $return_values[0] = $value;
          break;
        }
      }
      foreach ($relation_type->target_bundles as $bundle_key) {
        list($target_entity_type) = explode(':', $bundle_key);
        if ($target_entity_type == $value
          ->type()) {
          $return_values[1] = $value;
          break;
        }
      }
    }
  }
  return empty($return_values) ? $values : $return_values;
}