cck_phone.feeds.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. /**
  3. * @file
  4. * Implements Feeds mapping API
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. */
  9. function cck_phone_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'] == 'phone_number') {
  13. $targets[$name .':country_codes'] = array(
  14. 'name' => t('!label - country code', array('!label' => check_plain($instance['label']))),
  15. 'callback' => 'cck_phone_feeds_set_target',
  16. 'description' => t('The @label field.', array('@label' => $instance['label'])),
  17. );
  18. $targets[$name .':number'] = array(
  19. 'name' => t('!label - phone number', array('!label' => check_plain($instance['label']))),
  20. 'callback' => 'cck_phone_feeds_set_target',
  21. 'description' => t('The @label field.', array('@label' => $instance['label'])),
  22. );
  23. $targets[$name .':extension'] = array(
  24. 'name' => t('!label - extension', array('!label' => check_plain($instance['label']))),
  25. 'callback' => 'cck_phone_feeds_set_target',
  26. 'description' => t('The @label field.', array('@label' => $instance['label'])),
  27. );
  28. }
  29. }
  30. }
  31. /**
  32. * Callback for feed mapping.
  33. */
  34. function cck_phone_feeds_set_target($source, $entity, $target, $value) {
  35. if (empty($value)) {
  36. return;
  37. }
  38. // Handle non-multiple value fields.
  39. if (!is_array($value)) {
  40. $value = array($value);
  41. }
  42. // Iterate over all values.
  43. $i = 0;
  44. $info = field_info_field($target);
  45. list($field_name, $sub_field) = explode(':', $target);
  46. // We will call this multiple times, preserve existing values.
  47. $field = empty($entity->{$field_name}) ? array() : $entity->{$field_name};
  48. foreach ($value as $v) {
  49. if ($sub_field == 'country_codes') {
  50. $v = strtolower($v);
  51. }
  52. if (!is_array($v) && !is_object($v)) {
  53. $field['und'][$i][$sub_field] = $v;
  54. }
  55. if ($info['cardinality'] == 1) {
  56. break;
  57. }
  58. $i++;
  59. }
  60. $entity->{$field_name} = $field;
  61. }