file.inc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * @file
  4. * On behalf implementation of Feeds mapping API for file.module and
  5. * image.module.
  6. */
  7. /**
  8. * Implements hook_feeds_processor_targets().
  9. */
  10. function file_feeds_processor_targets($entity_type, $bundle_name) {
  11. $targets = array();
  12. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  13. $info = field_info_field($name);
  14. if (in_array($info['type'], array('file', 'image'))) {
  15. $targets[$name . ':uri'] = array(
  16. 'name' => t('@label: URI', array('@label' => $instance['label'])),
  17. 'callback' => 'file_feeds_set_target',
  18. 'description' => t('The URI of the @label field.', array('@label' => $instance['label'])),
  19. 'real_target' => $name,
  20. );
  21. // Keep the old target name for backwards compatibility, but hide it from
  22. // the UI.
  23. $targets[$name] = $targets[$name . ':uri'];
  24. $targets[$name]['deprecated'] = TRUE;
  25. if ($info['type'] == 'image') {
  26. $targets[$name . ':alt'] = array(
  27. 'name' => t('@label: Alt', array('@label' => $instance['label'])),
  28. 'callback' => 'file_feeds_set_target',
  29. 'description' => t('The alt tag of the @label field.', array('@label' => $instance['label'])),
  30. 'real_target' => $name,
  31. );
  32. $targets[$name . ':title'] = array(
  33. 'name' => t('@label: Title', array('@label' => $instance['label'])),
  34. 'callback' => 'file_feeds_set_target',
  35. 'description' => t('The title of the @label field.', array('@label' => $instance['label'])),
  36. 'real_target' => $name,
  37. );
  38. }
  39. elseif ($info['type'] === 'file') {
  40. $targets[$name . ':description'] = array(
  41. 'name' => t('@label: Description', array('@label' => $instance['label'])),
  42. 'callback' => 'file_feeds_set_target',
  43. 'description' => t('The description of the @label field.', array('@label' => $instance['label'])),
  44. 'real_target' => $name,
  45. );
  46. }
  47. }
  48. }
  49. return $targets;
  50. }
  51. /**
  52. * Callback for mapping file fields.
  53. */
  54. function file_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  55. $language = $mapping['language'];
  56. // Add default of uri for backwards compatibility.
  57. list($field_name, $sub_field) = explode(':', $target . ':uri');
  58. $info = field_info_field($field_name);
  59. if ($sub_field == 'uri') {
  60. foreach ($values as $k => $v) {
  61. if (!($v instanceof FeedsEnclosure)) {
  62. if (is_string($v)) {
  63. $values[$k] = new FeedsEnclosure($v, file_get_mimetype($v));
  64. }
  65. else {
  66. // Set the value for FALSE rather than remove it to keep our deltas
  67. // correct.
  68. $values[$k] = FALSE;
  69. }
  70. }
  71. }
  72. if ($entity instanceof Entity) {
  73. $entity_type = $entity->entityType();
  74. $bundle = $entity->bundle();
  75. }
  76. else {
  77. $entity_type = $source->importer->processor->entityType();
  78. $bundle = $source->importer->processor->bundle();
  79. }
  80. $instance_info = field_info_instance($entity_type, $field_name, $bundle);
  81. // Determine file destination.
  82. // @todo This needs review and debugging.
  83. $data = array();
  84. if (!empty($entity->uid)) {
  85. $data[$entity_type] = $entity;
  86. }
  87. $destination = file_field_widget_uri($info, $instance_info, $data);
  88. }
  89. // Populate entity.
  90. $field = isset($entity->$field_name) ? $entity->$field_name : array($language => array());
  91. $delta = 0;
  92. foreach ($values as $v) {
  93. if ($info['cardinality'] == $delta) {
  94. break;
  95. }
  96. if (!isset($field[$language][$delta])) {
  97. $field[$language][$delta] = array();
  98. }
  99. switch ($sub_field) {
  100. case 'alt':
  101. case 'title':
  102. case 'description':
  103. $field[$language][$delta][$sub_field] = $v;
  104. break;
  105. case 'uri':
  106. if ($v) {
  107. try {
  108. $v->setAllowedExtensions($instance_info['settings']['file_extensions']);
  109. $field[$language][$delta] += (array) $v->getFile($destination);
  110. // @todo: Figure out how to properly populate this field.
  111. $field[$language][$delta]['display'] = 1;
  112. }
  113. catch (Exception $e) {
  114. watchdog('feeds', check_plain($e->getMessage()));
  115. }
  116. }
  117. break;
  118. }
  119. $delta++;
  120. }
  121. $entity->$field_name = $field;
  122. }