diff --git a/mappers/date.inc b/mappers/date.inc index 8ff4cbb..944ef46 100644 --- a/mappers/date.inc +++ b/mappers/date.inc @@ -33,41 +33,28 @@ function date_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam } /** - * Implements hook_feeds_set_target(). - * - * @param $node - * The target node. - * @param $field_name - * The name of field on the target node to map to. - * @param $feed_element - * The value to be mapped. Should be either a (flexible) date string - * or a FeedsDateTimeElement object. - * + * Callback for setting target values. */ -function date_feeds_set_target($source, $entity, $target, $feed_element) { +function date_feeds_set_target($source, $entity, $target, array $values) { list($field_name, $sub_field) = explode(':', $target, 2); - if (!is_array($feed_element)) { - $feed_element = array($feed_element); - } - $delta = 0; - foreach ($feed_element as $f) { + foreach ($values as $value) { - if (!($feed_element instanceof FeedsDateTimeElement)) { + if (!($value instanceof FeedsDateTimeElement)) { - if (empty($f) || !is_numeric($f) && is_string($f) && !date_create($f)) { - $f = new FeedsDateTimeElement(NULL, NULL); + if (!strlen($value) || !is_numeric($value) && is_string($value) && !date_create($value)) { + $value = new FeedsDateTimeElement(NULL, NULL); } elseif ($sub_field == 'end') { - $f = new FeedsDateTimeElement(NULL, $f); + $value = new FeedsDateTimeElement(NULL, $value); } else { - $f = new FeedsDateTimeElement($f, NULL); + $value = new FeedsDateTimeElement($value, NULL); } } - $f->buildDateField($entity, $field_name, $delta); + $value->buildDateField($entity, $field_name, $delta); $delta++; } } diff --git a/mappers/file.inc b/mappers/file.inc index aa967b9..85d7ae2 100644 --- a/mappers/file.inc +++ b/mappers/file.inc @@ -48,57 +48,42 @@ function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function file_feeds_set_target($source, $entity, $target, $value) { - if (empty($value)) { - return; - } - - // Make sure $value is an array of objects of type FeedsEnclosure. - if (!is_array($value)) { - $value = array($value); - } - +function file_feeds_set_target($source, $entity, $target, array $values) { // Add default of uri for backwards compatibility. list($field_name, $sub_field) = explode(':', $target . ':uri'); $info = field_info_field($field_name); if ($sub_field == 'uri') { - foreach ($value as $k => $v) { + foreach ($values as $k => $v) { if (!($v instanceof FeedsEnclosure)) { if (is_string($v)) { - $value[$k] = new FeedsEnclosure($v, file_get_mimetype($v)); + $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v)); } else { - unset($value[$k]); + $values[$k] = FALSE; } } } - if (empty($value)) { - return; - } - static $destination; - if (!$destination) { - $entity_type = $source->importer->processor->entityType(); - $bundle = $source->importer->processor->bundle(); + $entity_type = $source->importer->processor->entityType(); + $bundle = $source->importer->processor->bundle(); - $instance_info = field_info_instance($entity_type, $field_name, $bundle); + $instance_info = field_info_instance($entity_type, $field_name, $bundle); - // Determine file destination. - // @todo This needs review and debugging. - $data = array(); - if (!empty($entity->uid)) { - $data[$entity_type] = $entity; - } - $destination = file_field_widget_uri($info, $instance_info, $data); + // Determine file destination. + // @todo This needs review and debugging. + $data = array(); + if (!empty($entity->uid)) { + $data[$entity_type] = $entity; } + $destination = file_field_widget_uri($info, $instance_info, $data); } // Populate entity. $field = isset($entity->$field_name) ? $entity->$field_name : array(LANGUAGE_NONE => array()); $delta = 0; - foreach ($value as $v) { + foreach ($values as $value) { if ($info['cardinality'] == $delta) { break; } @@ -110,18 +95,20 @@ function file_feeds_set_target($source, $entity, $target, $value) { switch ($sub_field) { case 'alt': case 'title': - $field[LANGUAGE_NONE][$delta][$sub_field] = $v; + $field[LANGUAGE_NONE][$delta][$sub_field] = $value; break; case 'uri': - try { - $file = $v->getFile($destination); - $field[LANGUAGE_NONE][$delta] += (array) $file; - // @todo: Figure out how to properly populate this field. - $field[LANGUAGE_NONE][$delta]['display'] = 1; - } - catch (Exception $e) { - watchdog_exception('Feeds', $e, nl2br(check_plain($e))); + if ($value) { + try { + $file = $value->getFile($destination); + $field[LANGUAGE_NONE][$delta] += (array) $file; + // @todo: Figure out how to properly populate this field. + $field[LANGUAGE_NONE][$delta]['display'] = 1; + } + catch (Exception $e) { + watchdog_exception('Feeds', $e, nl2br(check_plain($e))); + } } break; } diff --git a/mappers/link.inc b/mappers/link.inc index 45a86f7..d652818 100644 --- a/mappers/link.inc +++ b/mappers/link.inc @@ -41,15 +41,7 @@ function link_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function link_feeds_set_target($source, $entity, $target, $value) { - if (empty($value)) { - return; - } - - // Handle non-multiple value fields. - if (!is_array($value)) { - $value = array($value); - } +function link_feeds_set_target($source, $entity, $target, array $values) { // Iterate over all values. list($field_name, $column) = explode(':', $target); @@ -58,19 +50,20 @@ function link_feeds_set_target($source, $entity, $target, $value) { $field = isset($entity->$field_name) ? $entity->$field_name : array(); $delta = 0; - foreach ($value as $v) { + foreach ($values as $value) { if ($info['cardinality'] == $delta) { break; } - if (is_object($v) && ($v instanceof FeedsElement)) { - $v = $v->getValue(); + if (is_object($value) && ($value instanceof FeedsElement)) { + $value = $value->getValue(); } - if (is_scalar($v)) { - $field['und'][$delta][$column] = $v; + if (is_scalar($value)) { + $field['und'][$delta][$column] = $value; $delta++; } } + $entity->$field_name = $field; } diff --git a/mappers/number.inc b/mappers/number.inc index b64c4ee..bd003f3 100644 --- a/mappers/number.inc +++ b/mappers/number.inc @@ -37,14 +37,7 @@ function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_n * * Ensure that $value is a numeric to avoid database errors. */ -function number_feeds_set_target($source, $entity, $target, $value) { - - // Do not perform the regular empty() check here. 0 is a valid value. That's - // really just a performance thing anyway. - - if (!is_array($value)) { - $value = array($value); - } +function number_feeds_set_target($source, $entity, $target, array $values) { $info = field_info_field($target); @@ -54,18 +47,18 @@ function number_feeds_set_target($source, $entity, $target, $value) { // Allow for multiple mappings to the same target. $delta = count($field['und']); - foreach ($value as $v) { + foreach ($values as $value) { if ($info['cardinality'] == $delta) { break; } - if (is_object($v) && ($v instanceof FeedsElement)) { - $v = $v->getValue(); + if (is_object($value) && ($value instanceof FeedsElement)) { + $value = $value->getValue(); } - if (is_numeric($v)) { - $field['und'][$delta]['value'] = $v; + if (is_numeric($value)) { + $field['und'][$delta]['value'] = $value; $delta++; } } diff --git a/mappers/path.inc b/mappers/path.inc index 47ae0fc..d5b2b19 100644 --- a/mappers/path.inc +++ b/mappers/path.inc @@ -33,14 +33,16 @@ function path_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam * user has decided to map to and $value contains the value of the feed item * element the user has picked as a source. */ -function path_feeds_set_target($source, $entity, $target, $value, $mapping) { - if (empty($value)) { - $value = ''; - } +function path_feeds_set_target($source, $entity, $target, array $values, $mapping) { + + $alias = FALSE; - // Path alias cannot be multi-valued, so use the first value. - if (is_array($value)) { - $value = $value[0]; + // Path alias cannot be multi-valued, so use the first non-empty value. + foreach ($values as $value) { + if (strlen(ltrim(trim($value)), '/')) { + $alias = $value; + break; + } } $entity->path = array(); @@ -61,11 +63,11 @@ function path_feeds_set_target($source, $entity, $target, $value, $mapping) { $entity->path['pathauto'] = FALSE; // Allow pathauto to set the path alias if the option is set, and this value // is empty. - if (!empty($mapping['pathauto_override']) && !$value) { + if (!empty($mapping['pathauto_override']) && $alias === FALSE) { $entity->path['pathauto'] = TRUE; } else { - $entity->path['alias'] = ltrim($value, '/'); + $entity->path['alias'] = ltrim($alias, '/'); } } diff --git a/mappers/profile.inc b/mappers/profile.inc index 0dd62de..00fdf3a 100644 --- a/mappers/profile.inc +++ b/mappers/profile.inc @@ -30,6 +30,6 @@ function profile_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_ /** * Set the user profile target after import. */ -function profile_feeds_set_target($source, $entity, $target, $value, $mapping) { - $entity->$target = $value; +function profile_feeds_set_target($source, $entity, $target, array $values, $mapping) { + $entity->$target = reset($values); } diff --git a/mappers/taxonomy.inc b/mappers/taxonomy.inc index 02ac54e..f017038 100644 --- a/mappers/taxonomy.inc +++ b/mappers/taxonomy.inc @@ -77,17 +77,7 @@ function taxonomy_feeds_processor_targets_alter(&$targets, $entity_type, $bundle * * @todo Do not create new terms for non-autotag fields. */ -function taxonomy_feeds_set_target($source, $entity, $target, $terms, $mapping = array()) { - - // Allow mapping the string '0' to a term name. - if (empty($terms) && $terms != 0) { - return; - } - - // Handle non-multiple values. - if (!is_array($terms)) { - $terms = array($terms); - } +function taxonomy_feeds_set_target($source, $entity, $target, array $terms, $mapping = array()) { // Add in default values. $mapping += array( diff --git a/mappers/text.inc b/mappers/text.inc index 48447d7..75ac778 100644 --- a/mappers/text.inc +++ b/mappers/text.inc @@ -33,14 +33,7 @@ function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_nam /** * Callback for mapping text fields. */ -function text_feeds_set_target($source, $entity, $target, $value) { - if (empty($value)) { - return; - } - - if (!is_array($value)) { - $value = array($value); - } +function text_feeds_set_target($source, $entity, $target, array $values) { if (isset($source->importer->processor->config['input_format'])) { $format = $source->importer->processor->config['input_format']; @@ -54,18 +47,18 @@ function text_feeds_set_target($source, $entity, $target, $value) { // Allow for multiple mappings to the same target. $delta = count($field['und']); - foreach ($value as $v) { + foreach ($values as $value) { if ($info['cardinality'] == $delta) { break; } - if (is_object($v) && ($v instanceof FeedsElement)) { - $v = $v->getValue(); + if (is_object($value) && ($value instanceof FeedsElement)) { + $value = $value->getValue(); } - if (is_scalar($v)) { - $field['und'][$delta]['value'] = $v; + if (is_scalar($value)) { + $field['und'][$delta]['value'] = $value; if (isset($format)) { $field['und'][$delta]['format'] = $format; diff --git a/plugins/FeedsProcessor.inc b/plugins/FeedsProcessor.inc index c2b0af5..deb9f82 100755 --- a/plugins/FeedsProcessor.inc +++ b/plugins/FeedsProcessor.inc @@ -499,6 +499,9 @@ abstract class FeedsProcessor extends FeedsPlugin { else { $value = $parser->getSourceElement($source, $result, $mapping['source']); } + if (!is_array($value)) { + $value = array($value); + } // Map the source element's value to the target. if (isset($targets[$this->id][$mapping['target']]) &&