From 9cbd3945926993f62d740066fdc7512022b3f583 Mon Sep 17 00:00:00 2001 From: gaelg Date: Fri, 6 Jul 2012 16:21:19 +0200 Subject: [PATCH] =?UTF-8?q?Issue=201616680=20by=20jrao,=20Ga=C3=ABlG:=20Adde?= =?UTF-8?q?d=20mapping=20keys.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- entityreference.feeds.inc | 68 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 59 insertions(+), 9 deletions(-) diff --git a/entityreference.feeds.inc b/entityreference.feeds.inc index 093fa5f..87264c7 100644 --- a/entityreference.feeds.inc +++ b/entityreference.feeds.inc @@ -16,14 +16,42 @@ function entityreference_feeds_processor_targets_alter(&$targets, $entity_type, $name => $instance) { $info = field_info_field($name); if ($info['type'] == 'entityreference') { + // We don't use ":guid" in key, not to break existing configurations. $targets[$name] = array( - 'name' => check_plain($instance['label']), + 'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds GUID)')), 'callback' => 'entityreference_feeds_set_target', - 'description' => t('The field instance @label of @id', array( + 'description' => t('The field instance @label of @id matched by Feeds GUID.', array( '@label' => $instance['label'], '@id' => $name, )), ); + $targets[$name . ':url'] = array( + 'name' => check_plain($instance['label'] . t(' (Entity reference by Feeds URL)')), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The field instance @label of @id matched by Feeds URL.', array( + '@label' => $instance['label'], + '@id' => $name, + )), + 'real_target' => $name, + ); + $targets[$name . ':etid'] = array( + 'name' => check_plain($instance['label'] . t(' (Entity reference by Entity ID)')), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The field instance @label of @id matched by Entity ID.', array( + '@label' => $instance['label'], + '@id' => $name, + )), + 'real_target' => $name, + ); + $targets[$name . ':label'] = array( + 'name' => check_plain($instance['label'] . t(' (Entity reference by Entity label)')), + 'callback' => 'entityreference_feeds_set_target', + 'description' => t('The field instance @label of @id matched by Entity label.', array( + '@label' => $instance['label'], + '@id' => $name, + )), + 'real_target' => $name, + ); } } } @@ -62,8 +90,19 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma $values = array($value); } + // Determine the field we are matching against. + if (strpos($target, ':') === false) { + $match_key = 'guid'; + } + else { + list($target, $match_key) = explode(':', $target, 2); + } + // Get some useful field information. $info = field_info_field($target); + if ($match_key == 'label') { + $handler = entityreference_get_selection_handler($info); + } // Set the language of the field depending on the mapping. $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; @@ -75,13 +114,24 @@ 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(); - + switch ($match_key) { + case 'guid': + case 'url': + // Fetch the entity ID resulting from the mapping table look-up. + $entity_id = db_query( + 'SELECT entity_id FROM {feeds_item} WHERE :key = :value', + array(':key' => $match_key, ':value' => $value) + )->fetchField(); + break; + case 'etid': + $entity_id = $value; + break; + case 'label': + $etids = array_keys($handler->getReferencableEntities($value, '=')); + // Use the first matching entity. + $entity_id = reset($etids); + break; + } /* * Only add a reference to an existing entity ID if there exists a * mapping between it and the provided GUID. In cases where no such -- 1.7.9.5