name.feeds.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @file
  4. * Implements Feeds mapping API.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. */
  9. function name_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  10. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  11. $info = field_info_field($name);
  12. if ($info['type'] == 'name') {
  13. foreach (_name_translations() as $key => $title) {
  14. if (array_key_exists($key, $info['columns'])) {
  15. $targets[$name . ':' . $key] = array(
  16. 'name' => t('@name !component', array('@name' => $instance['label'], '!component' => $title)),
  17. 'callback' => 'name_feeds_set_target',
  18. 'description' => t('The !component component of the @label field of the node.', array('@label' => $instance['label'], '!component' => $title)),
  19. );
  20. }
  21. }
  22. }
  23. }
  24. }
  25. /**
  26. * Callback for feed mapping.
  27. */
  28. function name_feeds_set_target($source, $entity, $target, $value) {
  29. if (empty($value)) {
  30. return;
  31. }
  32. // Handle non-multiple value fields.
  33. if (!is_array($value)) {
  34. $value = array($value);
  35. }
  36. // Iterate over all values.
  37. $i = 0;
  38. $info = field_info_field($target);
  39. list($field_name, $sub_field) = explode(':', $target);
  40. // We will call this multiple times, preserve existing values.
  41. $field = empty($entity->{$field_name}) ? array() : $entity->{$field_name};
  42. foreach ($value as $v) {
  43. if (!is_array($v) && !is_object($v)) {
  44. $field[LANGUAGE_NONE][$i][$sub_field] = $v;
  45. }
  46. if ($info['cardinality'] == 1) {
  47. break;
  48. }
  49. $i++;
  50. }
  51. $entity->{$field_name} = $field;
  52. }