text.inc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for text.module.
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsProcessor::getMappingTargets()
  10. */
  11. function text_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. $text_types = array(
  13. 'list_text',
  14. 'text',
  15. 'text_long',
  16. 'text_with_summary',
  17. );
  18. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  19. $info = field_info_field($name);
  20. if (in_array($info['type'], $text_types)) {
  21. $targets[$name] = array(
  22. 'name' => check_plain($instance['label']),
  23. 'callback' => 'text_feeds_set_target',
  24. 'description' => t('The @label field of the entity.', array('@label' => $instance['label'])),
  25. );
  26. }
  27. }
  28. }
  29. /**
  30. * Callback for mapping text fields.
  31. */
  32. function text_feeds_set_target($source, $entity, $target, $value) {
  33. if (empty($value)) {
  34. return;
  35. }
  36. if (!is_array($value)) {
  37. $value = array($value);
  38. }
  39. if (isset($source->importer->processor->config['input_format'])) {
  40. $format = $source->importer->processor->config['input_format'];
  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_scalar($v)) {
  55. $field['und'][$delta]['value'] = $v;
  56. if (isset($format)) {
  57. $field['und'][$delta]['format'] = $format;
  58. }
  59. $delta++;
  60. }
  61. }
  62. $entity->$target = $field;
  63. }