danielebarchiesi@4: $instance) { danielebarchiesi@4: $info = field_info_field($name); danielebarchiesi@4: if ($info['type'] == 'entityreference') { danielebarchiesi@4: $targets[$name] = array( danielebarchiesi@4: 'name' => check_plain($instance['label']), danielebarchiesi@4: 'callback' => 'entityreference_feeds_set_target', danielebarchiesi@4: 'description' => t('The field instance @label of @id', array( danielebarchiesi@4: '@label' => $instance['label'], danielebarchiesi@4: '@id' => $name, danielebarchiesi@4: )), danielebarchiesi@4: ); danielebarchiesi@4: } danielebarchiesi@4: } danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: /** danielebarchiesi@4: * Entity reference callback for mapping. danielebarchiesi@4: * danielebarchiesi@4: * When the callback is invoked, $target contains the name of the field the danielebarchiesi@4: * user has decided to map to and $value contains the value of the feed item danielebarchiesi@4: * element the user has picked as a source. danielebarchiesi@4: * danielebarchiesi@4: * @param $source danielebarchiesi@4: * A FeedsSource object. danielebarchiesi@4: * @param $entity danielebarchiesi@4: * The entity to map to. danielebarchiesi@4: * @param $target danielebarchiesi@4: * The target key on $entity to map to. danielebarchiesi@4: * @param $value danielebarchiesi@4: * The value to map. MUST be an array. danielebarchiesi@4: * @param $mapping danielebarchiesi@4: * Array of mapping settings for current value. danielebarchiesi@4: * @param $input_format danielebarchiesi@4: * TRUE if an input format should be applied. danielebarchiesi@4: */ danielebarchiesi@4: function entityreference_feeds_set_target($source, $entity, $target, $value, $mapping, $input_format = FALSE) { danielebarchiesi@4: danielebarchiesi@4: // Don't do anything if we weren't given any data. danielebarchiesi@4: if (empty($value)) { danielebarchiesi@4: return; danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: // Assume that the passed in value could really be any number of values. danielebarchiesi@4: if (is_array($value)) { danielebarchiesi@4: $values = $value; danielebarchiesi@4: } danielebarchiesi@4: else { danielebarchiesi@4: $values = array($value); danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: // Get some useful field information. danielebarchiesi@4: $info = field_info_field($target); danielebarchiesi@4: danielebarchiesi@4: // Set the language of the field depending on the mapping. danielebarchiesi@4: $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE; danielebarchiesi@4: danielebarchiesi@4: // Iterate over all values. danielebarchiesi@4: $iterator = 0; danielebarchiesi@4: $field = isset($entity->$target) ? $entity->$target : array(); danielebarchiesi@4: foreach ($values as $value) { danielebarchiesi@4: danielebarchiesi@4: // Only process if this value was set for this instance. danielebarchiesi@4: if ($value) { danielebarchiesi@4: danielebarchiesi@4: // Fetch the entity ID resulting from the mapping table look-up. danielebarchiesi@4: $entity_id = db_query( danielebarchiesi@4: 'SELECT entity_id FROM {feeds_item} WHERE guid = :guid', danielebarchiesi@4: array(':guid' => $value) danielebarchiesi@4: )->fetchField(); danielebarchiesi@4: danielebarchiesi@4: /* danielebarchiesi@4: * Only add a reference to an existing entity ID if there exists a danielebarchiesi@4: * mapping between it and the provided GUID. In cases where no such danielebarchiesi@4: * mapping exists (yet), don't do anything here. There may be a mapping danielebarchiesi@4: * defined later in the CSV file. If so, and the user re-runs the import danielebarchiesi@4: * (as a second pass), we can add this reference then. (The "Update danielebarchiesi@4: * existing nodes" option must be selected during the second pass.) danielebarchiesi@4: */ danielebarchiesi@4: if ($entity_id) { danielebarchiesi@4: danielebarchiesi@4: // Assign the target ID. danielebarchiesi@4: $field[$language][$iterator]['target_id'] = $entity_id; danielebarchiesi@4: } danielebarchiesi@4: else /* there is no $entity_id, no mapping */ { danielebarchiesi@4: danielebarchiesi@4: /* danielebarchiesi@4: * Feeds stores a hash of every line imported from CSVs in order to danielebarchiesi@4: * make the import process more efficient by ignoring lines it's danielebarchiesi@4: * already seen. We need to short-circuit this process in this case danielebarchiesi@4: * because users may want to re-import the same line as an update later danielebarchiesi@4: * when (and if) a map to a reference exists. So in order to provide danielebarchiesi@4: * this opportunity later, we need to destroy the hash. danielebarchiesi@4: */ danielebarchiesi@4: unset($entity->feeds_item->hash); danielebarchiesi@4: } danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: // Break out of the loop if this field is single-valued. danielebarchiesi@4: if ($info['cardinality'] == 1) { danielebarchiesi@4: break; danielebarchiesi@4: } danielebarchiesi@4: $iterator++; danielebarchiesi@4: } danielebarchiesi@4: danielebarchiesi@4: // Add the field to the entity definition. danielebarchiesi@4: $entity->{$target} = $field; danielebarchiesi@4: }