diff --git a/entityreference.feeds.inc b/entityreference.feeds.inc index 093fa5f..aacb4d5 100644 --- a/entityreference.feeds.inc +++ b/entityreference.feeds.inc @@ -15,15 +15,44 @@ function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, foreach (field_info_instances($entity_type, $bundle_name) as $name => $instance) { $info = field_info_field($name); + require_once 'Log.php'; + Log::singleton('file', 'C:/Temp/php.log', 'php') + ->log("entityreference.feeds.inc entityreference_feeds_processor_targets_alter: ".print_r($info,true)); if ($info['type'] == 'entityreference') { - $targets[$name] = array( - 'name' => check_plain($instance['label']), - 'callback' => 'entityreference_feeds_set_target', - 'description' => t('The field instance @label of @id', array( - '@label' => $instance['label'], - '@id' => $name, - )), - ); + $targets[$name . ':eid'] = array( + 'name' => check_plain($instance['label']) . t(' (Entity reference by entity ID)'), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The @label field of the entity matched by entity ID.', array('@label' => $instance['label'])), + 'real_target' => $name); + + $targets[$name . ':url'] = array( + 'name' => check_plain($instance['label']) . t(' (Entity reference by Feeds URL)'), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The @label field of the entity matched by Feeds URL.', array('@label' => $instance['label'])), + 'real_target' => $name); + + $targets[$name . ':guid'] = array( + 'name' => check_plain($instance['label']) . t(' (Entity reference by Feeds GUID)'), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The @label field of the entity matched by Feeds GUID.', array('@label' => $instance['label'])), + 'real_target' => $name); + + switch ($info['settings']['target_type']) { + case 'node': + $targets[$name . ':title'] = array( + 'name' => $instance['label'] . t(' (Node reference by node title)'), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The @label field of the node matched by node title.', array('@label' => $instance['label'])), + 'real_target' => $name); + break; + case 'user': + $targets[$name . ':name'] = array( + 'name' => $instance['label'] . t(' (User reference by user name)'), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The @label field of the user matched by user name. ', array('@label' => $instance['label'])), + 'real_target' => $name); + break; + } } } } @@ -62,6 +91,9 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma $values = array($value); } + // Determine the field we are matching against. + list($target, $match_key) = explode(':', $target, 2); + // Get some useful field information. $info = field_info_field($target); @@ -76,11 +108,33 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma // Only process if this value was set for this instance. if ($value) { - // Fetch the entity ID resulting from the mapping table look-up. - $entity_id = db_query( - 'SELECT entity_id FROM {feeds_item} WHERE guid = :guid', - array(':guid' => $value) - )->fetchField(); + if ($match_key == 'guid' || $match_key == 'url') { + // get the value from table feeds-item. + $result = db_query('SELECT f.entity_id FROM {feeds_item} f WHERE f.'.$match_key.' = :v', array( ':v' => $value) ); + $entity_id = $result->fetchField(); + } + else { + $entity_type = $info['settings']['target_type']; + $query = new EntityFieldQuery(); + $query->entityCondition('entity_type', $entity_type); + switch ($match_key) { + case 'eid': + $query->entityCondition('entity_id', $value); + break; + case 'title': // node title + $query->entityCondition('bundle', array_keys($info['settings']['handler_settings']['target_bundles']), 'IN'); + $query->propertyCondition('title', $value); + break; + case 'name': // user name + $query->propertyCondition('name', $value); + break; + } + $result = $query->execute(); + if (isset($result[$entity_type])) { + $entity_ids = array_keys($result[$entity_type]); + $entity_id = array_shift($entity_ids); + } + } /* * Only add a reference to an existing entity ID if there exists a