You are here

function salesforce_pull_map_fields in Salesforce Suite 7.3

Map field values.

Parameters

array $field_maps: Array of field maps.

object $entity_wrapper: Entity wrapper object.

object $sf_object: sObject of the Salesforce record.

1 call to salesforce_pull_map_fields()
salesforce_pull_process_updated_records in modules/salesforce_pull/salesforce_pull.module
Process records in the queue.

File

modules/salesforce_pull/salesforce_pull.module, line 878
Pull updates from Salesforce when a Salesforce object is updated.

Code

function salesforce_pull_map_fields($field_maps, &$entity_wrapper, $sf_object) {
  $types = salesforce_mapping_get_fieldmap_types();
  foreach ($field_maps as $field_map) {
    $fieldmap_type = $field_map['drupal_field']['fieldmap_type'];
    if (!isset($types[$fieldmap_type]['pull_value_callback']) || !in_array($field_map['direction'], array(
      'sync',
      'sf_drupal',
    )) || !function_exists($types[$fieldmap_type]['pull_value_callback'])) {
      continue;
    }
    $drupal_fields_array = explode(':', $field_map['drupal_field']['fieldmap_value']);
    $parent = $entity_wrapper;
    foreach ($drupal_fields_array as $drupal_field) {
      if ($parent instanceof EntityListWrapper) {
        $child_wrapper = $parent
          ->get(0)->{$drupal_field};
      }
      else {
        $child_wrapper = $parent->{$drupal_field};
      }
      $parent = $child_wrapper;
    }
    $fieldmap_type = salesforce_mapping_get_fieldmap_types($field_map['drupal_field']['fieldmap_type']);
    $value = call_user_func($fieldmap_type['pull_value_callback'], $parent, $sf_object, $field_map);

    // Allow this value to be altered before assigning to the entity.
    drupal_alter('salesforce_pull_entity_value', $value, $field_map, $sf_object);
    try {
      if ($parent instanceof EntityListWrapper) {
        if (empty($value)) {
          $value = array();
        }
        elseif (!is_array($value)) {
          $value = array(
            $value,
          );
        }
      }
      if (empty($value) && count($drupal_fields_array) > 1 && empty($child_wrapper
        ->info()['parent']
        ->info()['required']) && !empty($child_wrapper
        ->info()['required'])) {

        // When a field with multiple properties (like a link field) has a
        // property that is required (url in the link field example) we cannot
        // set the property/child value to null. We can however set the parent
        // to null when it is not required. In case a field linked to a
        // property gets cleared in Salesforce we assume it's fine to clear the
        // parent of the property in Drupal.
        $child_wrapper
          ->info()['parent']
          ->set($value);
      }
      else {
        $parent
          ->set($value);
      }
    } catch (Exception $e) {
      $message = t('Exception during pull for @sfobj.@sffield @sfid to @dobj.@dprop @did with value @v: @e', array(
        '@sfobj' => $sf_object['attributes']['type'],
        '@sffield' => $field_map['salesforce_field']['name'],
        '@sfid' => $sf_object['Id'],
        '@dobj' => $entity_wrapper
          ->type(),
        '@dprop' => $field_map['drupal_field']['fieldmap_value'],
        '@did' => $entity_wrapper
          ->getIdentifier(),
        '@v' => $value,
        '@e' => $e
          ->getMessage(),
      ));
      throw new Exception($message, $e
        ->getCode(), $e);
    }
  }
}