You are here

function salesforce_mapping_property_fieldmap_push_value in Salesforce Suite 7.3

Value callback for property fieldmap type.

Parameters

array $fieldmap: Map of Drupal field to Salesforce field associations.

object $entity_wrapper: Entity wrapper object.

Return value

mixed The push value.

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

Code

function salesforce_mapping_property_fieldmap_push_value($fieldmap, $entity_wrapper) {
  $drupal_fields_array = explode(':', $fieldmap['drupal_field']['fieldmap_value']);
  $parent = $entity_wrapper;
  foreach ($drupal_fields_array as $drupal_field) {
    if ($parent instanceof EntityListWrapper) {

      // First list<> types, get the property from the first item.
      $child_wrapper = $parent
        ->get(0)->{$drupal_field};
    }
    else {
      $child_wrapper = $parent->{$drupal_field};
    }
    $parent = $child_wrapper;
  }
  $value = NULL;
  try {
    $value = $child_wrapper
      ->value();
  } catch (EntityMetadataWrapperException $e) {

    // Don't throw an exception, if we can't get the value, it probably means
    // the field is empty.
    // @TODO: Find a better way to check the validity of child wrappers.
  }

  // Salesforce wants arrays to be imploded strings.
  if (is_array($value)) {
    $value = implode(SALESFORCE_MAPPING_ARRAY_DELIMITER, $value);
  }

  // Field type specific handling.
  if ($child_wrapper
    ->type() == 'date' && isset($value) && ($fieldmap['salesforce_field']['type'] == 'date' || $fieldmap['salesforce_field']['type'] == 'datetime')) {

    // Drupal provides a timestamp, Salesforce wants ISO 8601 formatted
    // date/time.
    $value = format_date($value, 'custom', 'c');
  }

  // Boolean SF fields only want real boolean values. NULL is also not allowed.
  if ($fieldmap['salesforce_field']['type'] == 'boolean') {
    $value = (bool) $value;
  }
  return $value;
}