addressfield.feeds.inc 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @file
  4. * Integration with the Feeds module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. */
  9. function addressfield_feeds_processor_targets_alter(&$targets, $entity_type, $bundle) {
  10. foreach (field_info_instances($entity_type, $bundle) 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 $values
  35. * The value to populate the target with.
  36. */
  37. function addressfield_set_target($source, $entity, $target, $values) {
  38. list($field_name, $sub_field) = explode(':', $target, 2);
  39. // Convert the values into an array if it isn't one already to correspond to
  40. // Drupal's handling of field value arrays.
  41. if (!is_array($values)) {
  42. $values = array($values);
  43. }
  44. // If the field is already set on the given entity, update the existing value
  45. // array. Otherwise start with a fresh field value array.
  46. $field = isset($entity->$field_name) ? $entity->$field_name : array();
  47. // Loop over the field values array...
  48. foreach ($values as $delta => $value) {
  49. $field[LANGUAGE_NONE][$delta][$sub_field] = $value;
  50. }
  51. $entity->$field_name = $field;
  52. }