number.inc 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for number.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets().
  8. */
  9. function number_feeds_processor_targets($entity_type, $bundle_name) {
  10. $targets = array();
  11. $numeric_types = array(
  12. 'number_integer',
  13. 'number_decimal',
  14. 'number_float',
  15. );
  16. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  17. $info = field_info_field($name);
  18. if (in_array($info['type'], $numeric_types)) {
  19. $targets[$name] = array(
  20. 'name' => check_plain($instance['label']),
  21. 'callback' => 'number_feeds_set_target',
  22. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  23. );
  24. }
  25. }
  26. return $targets;
  27. }
  28. /**
  29. * Callback for mapping number fields.
  30. */
  31. function number_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  32. $language = $mapping['language'];
  33. // Iterate over all values.
  34. $field = isset($entity->$target) ? $entity->$target : array($language => array());
  35. foreach ($values as $value) {
  36. if (is_object($value) && ($value instanceof FeedsElement)) {
  37. $value = $value->getValue();
  38. }
  39. if (is_numeric($value)) {
  40. $field[$language][] = array('value' => $value);
  41. }
  42. }
  43. $entity->$target = $field;
  44. }