phone.feeds.inc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /**
  3. * @file
  4. * Implements Feeds support for Phone fields.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsNodeProcessor::getMappingTargets().
  10. */
  11. function phone_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  13. $info = field_info_field($name);
  14. if ($info['type'] == 'phone') {
  15. $targets[$name . ':url'] = array(
  16. 'name' => check_plain($instance['label']),
  17. 'callback' => 'phone_feeds_set_target',
  18. 'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
  19. );
  20. }
  21. }
  22. }
  23. /**
  24. * Callback for mapping. Here is where the actual mapping happens.
  25. *
  26. * When the callback is invoked, $target contains the name of the field the
  27. * user has decided to map to and $value contains the value of the feed item
  28. * element the user has picked as a source.
  29. */
  30. function phone_feeds_set_target($source, $entity, $target, $value) {
  31. if (empty($value)) {
  32. return;
  33. }
  34. // Handle non-multiple value fields.
  35. if (!is_array($value)) {
  36. $value = array($value);
  37. }
  38. // Iterate over all values.
  39. $i = 0;
  40. $info = field_info_field($target);
  41. list($field_name, $sub_field) = explode(':', $target);
  42. foreach ($value as $v) {
  43. if (empty($v[0])) {
  44. continue;
  45. }
  46. if (!is_array($v) && !is_object($v)) {
  47. $field['und'][$i]['value'] = $v;
  48. }
  49. if ($info['cardinality'] == 1) {
  50. break;
  51. }
  52. $i++;
  53. }
  54. $entity->{$field_name} = $field;
  55. }