addressfield.feeds.inc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * Integration with the Feeds module.
  5. */
  6. /**
  7. * Implements hook_feeds_node_processor_targets_alter().
  8. */
  9. function addressfield_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'] == 'addressfield') {
  13. foreach ($info['columns'] as $sub_field => $schema_info) {
  14. $name_label = $instance['label'] . ': ' . drupal_ucfirst(str_replace('_', ' ', $sub_field));
  15. $targets[$name . ':' . $sub_field] = array(
  16. 'name' => $name_label,
  17. 'callback' => 'addressfield_set_target',
  18. 'real_target' => $info['field_name'],
  19. 'description' => $schema_info['description'],
  20. );
  21. }
  22. }
  23. }
  24. }
  25. /**
  26. * Callback for hook_feeds_processor_targets_alter().
  27. *
  28. * @param $source
  29. * Field mapper source settings.
  30. * @param $entity
  31. * An entity object, for instance a node object.
  32. * @param $target
  33. * A string identifying the target on the node.
  34. * @param $value
  35. * The value to populate the target with.
  36. */
  37. function addressfield_set_target($source, $entity, $target, $value) {
  38. list($field_name, $sub_field) = explode(':', $target, 2);
  39. // Handle non-multiple value fields.
  40. if (!is_array($value)) {
  41. $value = array($value);
  42. }
  43. $field = isset($entity->$field_name) ? $entity->$field_name : array();
  44. foreach ($value as $i => $v) {
  45. $field['und'][$i][$sub_field] = $v;
  46. }
  47. $entity->$field_name = $field;
  48. }