You are here

function physical_feeds_set_target in Physical Fields 7

Callback for hook_feeds_processor_targets_alter().

Parameters

$source: Field mapper source settings.

$entity: An entity object, for instance a node object.

$target: A string identifying the target on the node.

$value: The value to populate the target with.

1 string reference to 'physical_feeds_set_target'
physical_feeds_processor_targets_alter in ./physical.feeds.inc
Implements hook_feeds_processor_targets_alter().

File

./physical.feeds.inc, line 47
Feeds processor hooks for importing physical fields using the Feeds module.

Code

function physical_feeds_set_target($source, $entity, $target, $value) {
  list($field_name, $sub_field) = explode(':', $target, 2);

  // Handle non-multiple value fields.
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }
  $info = field_info_field($field_name);
  $field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
  $entity_type = $source->importer->processor
    ->entityType();
  list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  $info_instance = field_info_instance($entity_type, $field_name, $bundle);

  //Check if default values exist
  if (isset($info_instance['default_value'][0])) {
    $default_values = $info_instance['default_value'][0];
  }
  else {
    $default_values = array(
      'length' => 0,
      'height' => 0,
      'width' => 0,
      'weight' => 0,
      'volume' => 0,
      'unit' => $info_instance['widget']['settings']['default_unit'],
    );
  }
  foreach ($value as $i => $v) {

    // Use default values incase they are not defined by mapper
    if (!isset($field[LANGUAGE_NONE][$i])) {
      foreach ($default_values as $default_sub_field => $default_value) {
        $field[LANGUAGE_NONE][$i][$default_sub_field] = $default_value;
      }
    }
    if ($sub_field != 'unit') {
      $field[LANGUAGE_NONE][$i][$sub_field] = !empty($v) && is_numeric(trim($v)) ? trim($v) : $default_values[$sub_field];
    }
    else {
      $field[LANGUAGE_NONE][$i][$sub_field] = !empty($v) ? trim($v) : $default_values[$sub_field];
    }
    if ($info['cardinality'] == 1) {
      break;
    }
  }
  $entity->{$field_name} = $field;
}