email.feeds.inc 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * Integration with the Feeds module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsNodeProcessor::getMappingTargets().
  10. */
  11. function email_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 (in_array($info['type'], array('email'))) {
  15. $targets[$name] = array(
  16. 'name' => $instance['label'],
  17. 'callback' => 'email_feeds_set_target',
  18. 'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
  19. );
  20. }
  21. }
  22. }
  23. /**
  24. * Callback function for mapping email field.
  25. *
  26. * This function is invoked via hook_feeds_processor_targets_alter().
  27. * Here is where the actual mapping happens.
  28. *
  29. * @param $target
  30. * the name of the field the user has decided to map to.
  31. * @param $value
  32. * the value of the feed item element the user has picked as a source.
  33. */
  34. function email_feeds_set_target($source, $entity, $target, $value) {
  35. $value = is_array($value) ? $value : array($value);
  36. $info = field_info_field($target);
  37. // Iterate over all values.
  38. $i = 0;
  39. $field = isset($entity->$target) ? $entity->$target : array();
  40. foreach ($value as $v) {
  41. if (!is_array($v) && !is_object($v)) {
  42. $field[LANGUAGE_NONE][$i]['email'] = $v;
  43. }
  44. if ($info['cardinality'] == 1) {
  45. break;
  46. }
  47. $i++;
  48. }
  49. $entity->{$target} = $field;
  50. }