number.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for number.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsProcessor::getMappingTargets()
  10. */
  11. function number_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. $numeric_types = array(
  13. 'list_integer',
  14. 'list_float',
  15. 'list_boolean',
  16. 'number_integer',
  17. 'number_decimal',
  18. 'number_float',
  19. );
  20. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  21. $info = field_info_field($name);
  22. if (in_array($info['type'], $numeric_types)) {
  23. $targets[$name] = array(
  24. 'name' => check_plain($instance['label']),
  25. 'callback' => 'number_feeds_set_target',
  26. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  27. );
  28. }
  29. }
  30. }
  31. /**
  32. * Callback for mapping numerics.
  33. *
  34. * Ensure that $value is a numeric to avoid database errors.
  35. */
  36. function number_feeds_set_target($source, $entity, $target, $value) {
  37. // Do not perform the regular empty() check here. 0 is a valid value. That's
  38. // really just a performance thing anyway.
  39. if (!is_array($value)) {
  40. $value = array($value);
  41. }
  42. $info = field_info_field($target);
  43. // Iterate over all values.
  44. $field = isset($entity->$target) ? $entity->$target : array('und' => array());
  45. // Allow for multiple mappings to the same target.
  46. $delta = count($field['und']);
  47. foreach ($value as $v) {
  48. if ($info['cardinality'] == $delta) {
  49. break;
  50. }
  51. if (is_object($v) && ($v instanceof FeedsElement)) {
  52. $v = $v->getValue();
  53. }
  54. if (is_numeric($v)) {
  55. $field['und'][$delta]['value'] = $v;
  56. $delta++;
  57. }
  58. }
  59. $entity->$target = $field;
  60. }