entityreference.feeds.inc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. // We don't use ":guid" in key, not to break existing configurations.
  16. $targets[$name] = array(
  17. 'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds GUID)')),
  18. 'callback' => 'entityreference_feeds_set_target',
  19. 'description' => t('The field instance @label of @id matched by Feeds GUID.', array(
  20. '@label' => $instance['label'],
  21. '@id' => $name,
  22. )),
  23. );
  24. $targets[$name . ':url'] = array(
  25. 'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds URL)')),
  26. 'callback' => 'entityreference_feeds_set_target',
  27. 'description' => t('The field instance @label of @id matched by Feeds URL.', array(
  28. '@label' => $instance['label'],
  29. '@id' => $name,
  30. )),
  31. 'real_target' => $name,
  32. );
  33. $targets[$name . ':etid'] = array(
  34. 'name' => check_plain($instance['label'] . t(' (Entity reference by Entity ID)')),
  35. 'callback' => 'entityreference_feeds_set_target',
  36. 'description' => t('The field instance @label of @id matched by Entity ID.', array(
  37. '@label' => $instance['label'],
  38. '@id' => $name,
  39. )),
  40. 'real_target' => $name,
  41. );
  42. $targets[$name . ':label'] = array(
  43. 'name' => check_plain($instance['label'] . t(' (Entity reference by Entity label)')),
  44. 'callback' => 'entityreference_feeds_set_target',
  45. 'description' => t('The field instance @label of @id matched by Entity label.', array(
  46. '@label' => $instance['label'],
  47. '@id' => $name,
  48. )),
  49. 'real_target' => $name,
  50. );
  51. }
  52. }
  53. }
  54. /**
  55. * Entity reference callback for hook_feeds_processor_targets().
  56. */
  57. function entityreference_feeds_set_target(FeedsSource $source, $entity, $target, array $values, array $mapping) {
  58. // Don't do anything if we weren't given any data.
  59. if (empty($values)) {
  60. return;
  61. }
  62. // Determine the field we are matching against.
  63. if (strpos($target, ':') === FALSE) {
  64. $match_key = 'guid';
  65. }
  66. else {
  67. list($target, $match_key) = explode(':', $target, 2);
  68. }
  69. // Get some useful field information.
  70. $info = field_info_field($target);
  71. if ($match_key == 'label') {
  72. $handler = entityreference_get_selection_handler($info);
  73. }
  74. // Set the language of the field depending on the mapping.
  75. $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
  76. // Iterate over all values.
  77. $iterator = 0;
  78. $field = isset($entity->$target) ? $entity->$target : array();
  79. foreach ($values as $value) {
  80. // Only process if this value was set for this instance.
  81. if ($value) {
  82. switch ($match_key) {
  83. case 'guid':
  84. case 'url':
  85. // Fetch the entity ID resulting from the mapping table look-up.
  86. $entity_id = db_select('feeds_item', 'fi')
  87. ->fields('fi', array('entity_id'))
  88. ->condition($match_key, $value, '=')
  89. ->execute()
  90. ->fetchField();
  91. break;
  92. case 'etid':
  93. $entity_id = $value;
  94. break;
  95. case 'label':
  96. $options = $handler->getReferencableEntities($value, '=');
  97. if ($options) {
  98. $options = reset($options);
  99. $etids = array_keys($options);
  100. // Use the first matching entity.
  101. $entity_id = reset($etids);
  102. }
  103. else {
  104. $entity_id = NULL;
  105. }
  106. break;
  107. }
  108. /*
  109. * Only add a reference to an existing entity ID if there exists a
  110. * mapping between it and the provided GUID. In cases where no such
  111. * mapping exists (yet), don't do anything here. There may be a mapping
  112. * defined later in the CSV file. If so, and the user re-runs the import
  113. * (as a second pass), we can add this reference then. (The "Update
  114. * existing nodes" option must be selected during the second pass.)
  115. */
  116. if ($entity_id) {
  117. // Assign the target ID.
  118. $field[$language][$iterator]['target_id'] = $entity_id;
  119. }
  120. else /* there is no $entity_id, no mapping */ {
  121. /*
  122. * Feeds stores a hash of every line imported from CSVs in order to
  123. * make the import process more efficient by ignoring lines it's
  124. * already seen. We need to short-circuit this process in this case
  125. * because users may want to re-import the same line as an update later
  126. * when (and if) a map to a reference exists. So in order to provide
  127. * this opportunity later, we need to destroy the hash.
  128. */
  129. unset($entity->feeds_item->hash);
  130. $source->log('entityreference', t('No existing entity found for entity @source_id entityreference to source entity @value', array('@source_id' => $entity->feeds_item->entity_id, '@value' => $value)));
  131. }
  132. }
  133. // Break out of the loop if this field is single-valued.
  134. if ($info['cardinality'] == 1) {
  135. break;
  136. }
  137. $iterator++;
  138. }
  139. // Add the field to the entity definition.
  140. $entity->{$target} = $field;
  141. }