entityreference.feeds.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 mapping.
  56. *
  57. * When the callback is invoked, $target contains the name of the field the
  58. * user has decided to map to and $value contains the value of the feed item
  59. * element the user has picked as a source.
  60. *
  61. * @param $source
  62. * A FeedsSource object.
  63. * @param $entity
  64. * The entity to map to.
  65. * @param $target
  66. * The target key on $entity to map to.
  67. * @param $value
  68. * The value to map. MUST be an array.
  69. */
  70. function entityreference_feeds_set_target($source, $entity, $target, $value) {
  71. // Don't do anything if we weren't given any data.
  72. if (empty($value)) {
  73. return;
  74. }
  75. // Assume that the passed in value could really be any number of values.
  76. if (is_array($value)) {
  77. $values = $value;
  78. }
  79. else {
  80. $values = array($value);
  81. }
  82. // Determine the field we are matching against.
  83. if (strpos($target, ':') === FALSE) {
  84. $match_key = 'guid';
  85. }
  86. else {
  87. list($target, $match_key) = explode(':', $target, 2);
  88. }
  89. // Get some useful field information.
  90. $info = field_info_field($target);
  91. if ($match_key == 'label') {
  92. $handler = entityreference_get_selection_handler($info);
  93. }
  94. // Set the language of the field depending on the mapping.
  95. $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
  96. // Iterate over all values.
  97. $iterator = 0;
  98. $field = isset($entity->$target) ? $entity->$target : array();
  99. foreach ($values as $value) {
  100. // Only process if this value was set for this instance.
  101. if ($value) {
  102. switch ($match_key) {
  103. case 'guid':
  104. case 'url':
  105. // Fetch the entity ID resulting from the mapping table look-up.
  106. $entity_id = db_select('feeds_item', 'fi')
  107. ->fields('fi', array('entity_id'))
  108. ->condition($match_key, $value,'=')
  109. ->execute()
  110. ->fetchField();
  111. break;
  112. case 'etid':
  113. $entity_id = $value;
  114. break;
  115. case 'label':
  116. $options = $handler->getReferencableEntities($value, '=');
  117. $options = reset($options);
  118. $etids = array_keys($options);
  119. // Use the first matching entity.
  120. $entity_id = reset($etids);
  121. break;
  122. }
  123. /*
  124. * Only add a reference to an existing entity ID if there exists a
  125. * mapping between it and the provided GUID. In cases where no such
  126. * mapping exists (yet), don't do anything here. There may be a mapping
  127. * defined later in the CSV file. If so, and the user re-runs the import
  128. * (as a second pass), we can add this reference then. (The "Update
  129. * existing nodes" option must be selected during the second pass.)
  130. */
  131. if ($entity_id) {
  132. // Assign the target ID.
  133. $field[$language][$iterator]['target_id'] = $entity_id;
  134. }
  135. else /* there is no $entity_id, no mapping */ {
  136. /*
  137. * Feeds stores a hash of every line imported from CSVs in order to
  138. * make the import process more efficient by ignoring lines it's
  139. * already seen. We need to short-circuit this process in this case
  140. * because users may want to re-import the same line as an update later
  141. * when (and if) a map to a reference exists. So in order to provide
  142. * this opportunity later, we need to destroy the hash.
  143. */
  144. unset($entity->feeds_item->hash);
  145. $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)));
  146. }
  147. }
  148. // Break out of the loop if this field is single-valued.
  149. if ($info['cardinality'] == 1) {
  150. break;
  151. }
  152. $iterator++;
  153. }
  154. // Add the field to the entity definition.
  155. $entity->{$target} = $field;
  156. }