list.inc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for list.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. */
  9. function list_feeds_processor_targets($entity_type, $bundle_name) {
  10. $targets = array();
  11. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  12. $info = field_info_field($name);
  13. switch ($info['type']) {
  14. case 'list_integer':
  15. case 'list_float':
  16. $targets[$name] = array(
  17. 'name' => check_plain($instance['label']),
  18. 'callback' => 'number_feeds_set_target',
  19. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  20. );
  21. break;
  22. case 'list_boolean':
  23. $targets[$name] = array(
  24. 'name' => check_plain($instance['label']),
  25. 'callback' => 'list_feeds_set_boolean_target',
  26. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  27. );
  28. break;
  29. case 'list_text':
  30. $targets[$name] = array(
  31. 'name' => check_plain($instance['label']),
  32. 'callback' => 'text_feeds_set_target',
  33. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  34. );
  35. break;
  36. }
  37. }
  38. return $targets;
  39. }
  40. /**
  41. * Callback for setting list_boolean fields.
  42. */
  43. function list_feeds_set_boolean_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  44. $language = $mapping['language'];
  45. $field = isset($entity->$target) ? $entity->$target : array($language => array());
  46. foreach ($values as $value) {
  47. if (is_object($value) && ($value instanceof FeedsElement)) {
  48. $value = $value->getValue();
  49. }
  50. if (is_string($value) && strlen($value) == 0) {
  51. // Don't convert an empty string to a boolean.
  52. continue;
  53. }
  54. if (is_null($value)) {
  55. // Don't convert a NULL value to a boolean.
  56. continue;
  57. }
  58. $field[$language][] = array('value' => (int) (bool) $value);
  59. }
  60. $entity->$target = $field;
  61. }