field.inc 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for field.module.
  5. */
  6. /**
  7. * Implements hook_feeds_presave().
  8. */
  9. function field_feeds_presave(FeedsSource $source, $entity, $item, $entity_id) {
  10. $entity_type = $entity->feeds_item->entity_type;
  11. // Not a real entity.
  12. if (!entity_get_info($entity_type)) {
  13. return;
  14. }
  15. // Gather the fields that Feeds is mapping to.
  16. $feeds_fields = array();
  17. foreach ($source->importer()->processor->getMappings() as $mapping) {
  18. list($field) = explode(':', $mapping['target']);
  19. $feeds_fields[$field] = TRUE;
  20. }
  21. list(, , $bundle) = entity_extract_ids($entity_type, $entity);
  22. foreach (field_info_instances($entity_type, $bundle) as $instance) {
  23. $field_name = $instance['field_name'];
  24. // Skip fields that Feeds isn't mapping to, and empty fields.
  25. if (!isset($feeds_fields[$field_name]) || empty($entity->$field_name) || !is_array($entity->$field_name)) {
  26. continue;
  27. }
  28. $info = field_info_field($field_name);
  29. foreach ($entity->$field_name as $language => $values) {
  30. // Filter out empty values.
  31. $values = _field_filter_items($info, $values);
  32. // Check that the number of values doesn't exceed the field cardinality.
  33. if ($info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && count($values) > $info['cardinality']) {
  34. $values = array_slice($values, 0, $info['cardinality']);
  35. }
  36. $entity->{$field_name}[$language] = $values;
  37. }
  38. }
  39. }