entityreference.feeds.inc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @file
  4. * Feeds mapping implementation for the Entity reference module
  5. */
  6. /**
  7. * Implements hook_feeds_processor_targets_alter().
  8. *
  9. * @see FeedsNodeProcessor::getMappingTargets().
  10. */
  11. function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  12. foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) {
  13. $info = field_info_field($name);
  14. if ($info['type'] == 'entityreference') {
  15. $targets[$name] = array(
  16. 'name' => check_plain($instance['label']),
  17. 'callback' => 'entityreference_feeds_set_target',
  18. 'description' => t('The field instance @label of @id', array(
  19. '@label' => $instance['label'],
  20. '@id' => $name,
  21. )),
  22. );
  23. }
  24. }
  25. }
  26. /**
  27. * Entity reference callback for mapping.
  28. *
  29. * When the callback is invoked, $target contains the name of the field the
  30. * user has decided to map to and $value contains the value of the feed item
  31. * element the user has picked as a source.
  32. *
  33. * @param $source
  34. * A FeedsSource object.
  35. * @param $entity
  36. * The entity to map to.
  37. * @param $target
  38. * The target key on $entity to map to.
  39. * @param $value
  40. * The value to map. MUST be an array.
  41. * @param $mapping
  42. * Array of mapping settings for current value.
  43. * @param $input_format
  44. * TRUE if an input format should be applied.
  45. */
  46. function entityreference_feeds_set_target($source, $entity, $target, $value, $mapping, $input_format = FALSE) {
  47. // Don't do anything if we weren't given any data.
  48. if (empty($value)) {
  49. return;
  50. }
  51. // Assume that the passed in value could really be any number of values.
  52. if (is_array($value)) {
  53. $values = $value;
  54. }
  55. else {
  56. $values = array($value);
  57. }
  58. // Get some useful field information.
  59. $info = field_info_field($target);
  60. // Set the language of the field depending on the mapping.
  61. $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
  62. // Iterate over all values.
  63. $iterator = 0;
  64. $field = isset($entity->$target) ? $entity->$target : array();
  65. foreach ($values as $value) {
  66. // Only process if this value was set for this instance.
  67. if ($value) {
  68. // Fetch the entity ID resulting from the mapping table look-up.
  69. $entity_id = db_query(
  70. 'SELECT entity_id FROM {feeds_item} WHERE guid = :guid',
  71. array(':guid' => $value)
  72. )->fetchField();
  73. /*
  74. * Only add a reference to an existing entity ID if there exists a
  75. * mapping between it and the provided GUID. In cases where no such
  76. * mapping exists (yet), don't do anything here. There may be a mapping
  77. * defined later in the CSV file. If so, and the user re-runs the import
  78. * (as a second pass), we can add this reference then. (The "Update
  79. * existing nodes" option must be selected during the second pass.)
  80. */
  81. if ($entity_id) {
  82. // Assign the target ID.
  83. $field[$language][$iterator]['target_id'] = $entity_id;
  84. }
  85. else /* there is no $entity_id, no mapping */ {
  86. /*
  87. * Feeds stores a hash of every line imported from CSVs in order to
  88. * make the import process more efficient by ignoring lines it's
  89. * already seen. We need to short-circuit this process in this case
  90. * because users may want to re-import the same line as an update later
  91. * when (and if) a map to a reference exists. So in order to provide
  92. * this opportunity later, we need to destroy the hash.
  93. */
  94. unset($entity->feeds_item->hash);
  95. }
  96. }
  97. // Break out of the loop if this field is single-valued.
  98. if ($info['cardinality'] == 1) {
  99. break;
  100. }
  101. $iterator++;
  102. }
  103. // Add the field to the entity definition.
  104. $entity->{$target} = $field;
  105. }