You are here

function sf_entity_import_field_default in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 7 sf_entity/sf_entity.module \sf_entity_import_field_default()
2 string references to 'sf_entity_import_field_default'
sf_entity_fieldmap_objects in sf_entity/sf_entity.module
sf_entity_import in sf_entity/sf_entity.module
Imports data from Salesforce into a Drupal entity

File

sf_entity/sf_entity.module, line 1306
Integrates fieldable entities with the Salesforce API.

Code

function sf_entity_import_field_default(&$entity, $fieldkey, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {

  // Get the language.
  $lang = _sf_entity_get_language($entity);

  // Get the data array for the field.
  list($fieldname, $column) = explode(':', $fieldkey, 2);
  if (empty($column)) {
    $column = 'value';
  }
  $data = array();
  if (property_exists($entity, $fieldname) && is_array($entity->{$fieldname})) {
    $data = $entity->{$fieldname};
  }

  // Don't try to do an import for a field that does't exist in the response from Salesforce.
  if (!isset($sf_data->{$sf_fieldname})) {
    return;
  }

  // Convert data based on what Salesforce type we're importing.
  // @todo: Also does conversions based on what types (checkbox, date field) it is assumed are being used
  // to map them in Drupal. Later, change that to be a separate case statement based on field_info_field().
  switch ($sf_field_definition['salesforce']['type']) {
    case 'multipicklist':

      // Salesforce sends multiple values as a semicolon-delimited string.
      // @todo: Determine how the field definition for multi-valued fields in Drupal is being set.
      if (isset($drupal_field_definition['multiple'])) {
        $sf_data = explode(';', $sf_data->{$sf_fieldname});
        foreach ($sf_data as $row) {
          $data[$lang][] = array(
            'value' => $row,
          );
        }
      }
      else {
        $data[$lang][0][$column] = $sf_data->{$sf_fieldname};
      }
      break;
    case 'boolean':

      // Drupal stores boolean fields as ints.
      // Trying to pass a boolean value (TRUE, FALSE) will cause a fatal database error.
      $data[$lang][0][$column] = (int) $sf_data->{$sf_fieldname};
      break;
    case 'date':
    case 'datetime':

      // Truncate the date to a length that can be saved.
      $data[$lang][0][$column] = substr($sf_data->{$sf_fieldname}, 0, 19);
      break;
    default:

      // Unless handled above in this switch, we don't yet handle fields with multiple values.
      $data[$lang][0][$column] = $sf_data->{$sf_fieldname};
      break;
  }
  $entity->{$fieldname} = $data;
}