file.inc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for file.module and
  5. * image.module.
  6. *
  7. * Does actually not include mappers for field types defined in fields module
  8. * (because there aren't any) but mappers for all fields that contain their
  9. * value simply in $entity->fieldname['und'][$i]['value'].
  10. */
  11. /**
  12. * Implements hook_feeds_processor_targets_alter().
  13. *
  14. * @see FeedsNodeProcessor::getMappingTargets().
  15. */
  16. function file_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  17. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  18. $info = field_info_field($name);
  19. if (in_array($info['type'], array('file', 'image'))) {
  20. $targets[$name] = array(
  21. 'name' => check_plain($instance['label']),
  22. 'callback' => 'file_feeds_set_target',
  23. 'description' => t('The @label field of the node.', array('@label' => $instance['label'])),
  24. );
  25. }
  26. }
  27. }
  28. /**
  29. * Callback for mapping. Here is where the actual mapping happens.
  30. *
  31. * When the callback is invoked, $target contains the name of the field the
  32. * user has decided to map to and $value contains the value of the feed item
  33. * element the user has picked as a source.
  34. */
  35. function file_feeds_set_target($source, $entity, $target, $value) {
  36. if (empty($value)) {
  37. return;
  38. }
  39. module_load_include('inc', 'file');
  40. // Make sure $value is an array of objects of type FeedsEnclosure.
  41. if (!is_array($value)) {
  42. $value = array($value);
  43. }
  44. foreach ($value as $k => $v) {
  45. if (!($v instanceof FeedsEnclosure)) {
  46. if (is_string($v)) {
  47. $value[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
  48. }
  49. else {
  50. unset($value[$k]);
  51. }
  52. }
  53. }
  54. if (empty($value)) {
  55. return;
  56. }
  57. // Determine file destination.
  58. // @todo This needs review and debugging.
  59. list($entity_id, $vid, $bundle_name) = entity_extract_ids($entity->feeds_item->entity_type, $entity);
  60. $instance_info = field_info_instance($entity->feeds_item->entity_type, $target, $bundle_name);
  61. $info = field_info_field($target);
  62. $data = array();
  63. if (!empty($entity->uid)) {
  64. $data[$entity->feeds_item->entity_type] = $entity;
  65. }
  66. $destination = file_field_widget_uri($info, $instance_info, $data);
  67. // Populate entity.
  68. $i = 0;
  69. $field = isset($entity->$target) ? $entity->$target : array();
  70. foreach ($value as $v) {
  71. try {
  72. $file = $v->getFile($destination);
  73. }
  74. catch (Exception $e) {
  75. watchdog_exception('Feeds', $e, nl2br(check_plain($e)));
  76. }
  77. if ($file) {
  78. $field['und'][$i] = (array)$file;
  79. $field['und'][$i]['display'] = 1; // @todo: Figure out how to properly populate this field.
  80. if ($info['cardinality'] == 1) {
  81. break;
  82. }
  83. $i++;
  84. }
  85. }
  86. $entity->{$target} = $field;
  87. }