addressfield.feeds.inc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. * @param array $mapping
  37. * Associative array of the mapping settings from the per mapping
  38. * configuration form.
  39. */
  40. function addressfield_set_target($source, $entity, $target, $values, $mapping) {
  41. $language = $mapping['language'];
  42. list($field_name, $sub_field) = explode(':', $target, 2);
  43. // Field info and instance are required for setting default values.
  44. $entity_type = $source->importer->processor->entityType();
  45. list(, , $bundle_name) = entity_extract_ids($entity_type, $entity);
  46. $info = field_info_field($field_name);
  47. $instance = field_info_instance($entity_type, $field_name, $bundle_name);
  48. // Convert the values into an array if it isn't one already to correspond to
  49. // Drupal's handling of field value arrays.
  50. if (!is_array($values)) {
  51. $values = array($values);
  52. }
  53. // If the field is already set on the given entity, update the existing value
  54. // array. Otherwise start with a fresh field value array.
  55. $field = isset($entity->{$field_name}) ? $entity->{$field_name} : array();
  56. // Loop over the field values array...
  57. foreach ($values as $delta => $value) {
  58. // Set defaults for new values.
  59. if (!isset($field[$language][$delta])) {
  60. $field[$language][$delta] = addressfield_default_values($info, $instance);
  61. }
  62. $field[$language][$delta][$sub_field] = $value;
  63. }
  64. $entity->{$field_name} = $field;
  65. }