It's my understanding that for Feeds to support node_reference and user_reference fields in Drupal 7, a mapper will be needed. This is effectively the same issue as #724536: Mapper for nodereference field in Drupal 6, but for Drupal 7 instead of Drupal 6. I'm bundling node_reference and user_reference together since (for now at least), support for both is being provided by the same module, namely References.

I've taken a first stab at creating the necessary mapper. (It's also my first time writing code for Drupal, so apologies for any standards I've overlooked). So far I've only done a few basic tests -- and I'm guessing that a proper suite of test functions will eventually be needed. But I wanted to post what I've done before going too much further in case there's any feedback.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Nephele’s picture

Version: 7.x-2.0-alpha2 » 7.x-2.x-dev
Assigned: Unassigned » Nephele
Status: Active » Needs work
FileSize
7.53 KB

I'm providing a new patch based on the most recent version from #724536: Mapper for nodereference field in Drupal 6 -- I based my original patch on an out-of-date version.

Also I've assigned the issue to myself to indicate that I'm planning to add the test class once I get the time.

Anonymous’s picture

Works like a charm. I tried it with a simple node reference (select list) with about 100 nodes. Mapping without a problem.

johnv’s picture

Status: Needs work » Reviewed & tested by the community

I used the patch with D7-RC3, and it works perfectly. Thanks!

johnv’s picture

After some testing, I cam across the following error:
If a line item of the import-file contains references to nodes that do not exist, an info-message is raised, and a new node is created for the incorrect item, resulting in corrupted/incomplete data, even if the nodereference is set to Required Field in the Content Type.
IMO in this case, the import-line should be rejected.

joshuajabbour’s picture

FYI, I reworked #724536: Mapper for nodereference field in Drupal 6 a bit, removing some issues. See comment #83.

attiks’s picture

I think this is RTBC

jackbravo’s picture

Subscribe

ropaolle’s picture

Subscribe

retolist’s picture

Subscribe

mattbk’s picture

superscribe

johnv’s picture

FileSize
3.46 KB

I'm very happy with Nephele's mapper. I have added support for GUID/URL.
Since my usecase is very limited, I have not touched anything as mentioned in #5.
Attached patch is a delta, so you should apply it on top of #1.

bambilicious’s picture

Subscribing.

mattbk’s picture

For the patched version, in what format is the input for the reference fields? Is it ID or title or a combination, or what? Just curious before I attempt to patch.

johnv’s picture

dakotaroar, I only use the GUID, don't know how to use the URL. My use case is:
- Feed 1: Create nodes of Content type A, setting the GUID/URL:

Book Title e.g. 'XML programming'
ISBN field_isbn e.g. '0-7645-3829-2'
ISBN GUID X e.g. '0-7645-3829-2

- Feed 2: Create nodes of Content type B, getting the GUID/URL of Feed 1:

Author Title e.g. 'B. Benz'
Author GUID X e.g. 'B. Benz'
Book field_book_ref (Node reference by Feeds GUID) e.g. '0-7645-3829-2

The advantage is that you can map to 'technical' fields. Users may change Titles, etc. without disrupting future feed imports.
(N.B. Somewhere out there is also a patch mapping to the node-field itself, bypassing the Feeds-GUID table, which is way better. This allows mapping to node created by users instead of feeds)

mattbk’s picture

NM the message below, I had applied the patch incorrectly and created the references.inc file in the feeds root directory, not the mappers directory. Looks better now, testing...
----------------------
Forgive me for just not being entirely familiar with how Feeds works, but these patches should allow me to map a field in an incoming file (a CSV, say) to a field that is a node reference, correct? When I'm setting up mappings, no node reference fields appear in the dropdown menu. I've run update.php and cleared the caches but I still don't see them. I was looking through the module code in references.inc but I'm not a module developer so I don't really know how it's supposed to go together.

Pounding my head against a wall, glad you guys (like johnv) are around to baby-step people like me through things! The funny part is that I managed to patch things correctly this time around.

funkeyrandy’s picture

Priority: Normal » Critical

great patch! but how would you tag to multiple node references items..for example:

two node reference author nodes tagged to a single article...im using csv import and can delimit by a comma or anything else...

is see you limit to one:

   // Lookup potential exact matches for the value (limit to one result).
          $matches = $validate_function($info, $v, 'equals', array(), 1);
          // Use the first element of the potential matches.
          $id = key($matches);

thanks!

funkeyrandy’s picture

nevermind! i figured it out....just change line 108

$value = array($value);
to

$value=explode(",", $value);

and separate by a comma in your csv

attiks’s picture

Can we get this in, and create a new issue for #17?

johnv’s picture

I am not sure either, if #17 is the correct patch for everyone.
Feeds does not seem to support multivalue fields, and separators can be different for everyone.
I used Feeds Tamper for multivalue fields; didn't try terms, but there are issues about that; node ref's is now the next case.
But is the fix works for someone, he should apply it. Thanks, funkeyrandy!

funkeyrandy’s picture

my code will work for people who have comma delimited output for node reference fields..however, i think the ultimate patch would be to provide users with the ability to choose what separator they want for their needs....i dont have time right now...but hopefully soon!

r

allella’s picture

Priority: Normal » Critical
Status: Needs work » Reviewed & tested by the community

I ran into an issue after applying patch #1 and #11.

Upon importing the following error came up

An AJAX HTTP error occurred. HTTP Result Code: 200 Debugging information follows. Path: /torque/batch?id=7&op=do StatusText: OK ResponseText: Fatal error: Call to undefined function _node_reference_potential_references() in /home/allella/public_html/torque/sites/all/modules/feeds/mappers/references.inc on line 203

I updated References module from an older -dev version to 7.x-2.0-beta3. Based on grep and #1150004: _node_reference_potential_references() signature changed in node_ref.module I updated line 153 of sites/all/modules/feeds/mappers/references.inc

from

    $validate_function = '_node_reference_potential_references';

to

    $validate_function = '_node_reference_potential_references_standard';

Not sure if that's a good fix, but it works for me. A better fix may involve updating the patches above to use node_reference_potential_references(), instead of _node_reference_potential_references_standard() or _node_reference_potential_references

Dave Reid’s picture

Priority: Critical » Normal
Status: Reviewed & tested by the community » Needs review

Status: Needs review » Needs work

The last submitted patch, references_by_guid.patch, failed testing.

johnv’s picture

Priority: Critical » Normal
Status: Reviewed & tested by the community » Needs work

@orangecoat-ciallella , Issue #1150004: _node_reference_potential_references() signature changed in node_ref.module deals with this API change. This is fixed in References-release may 15th or later.
Interpreting that patch, we should either be able to use #11 again, or use node_reference_potential_references instead.

TimelessDomain’s picture

Applied patches from #1 & #11. "Target" shows the node reference fields but "Source" does not. Currently taxonomy allows mapping from parent feed, but as of now references does not #1074662: Inherit properties from parent feed node (taxonomy, author, OG, language)

If someone wants some funds to get this issue & #1074662 solved, I would be willing to chip in

marcjpb’s picture

Hey guys, I did #1, followed by the patch at #11 and was able to import everything. I used GUID to link my child node to his parent.

The only problem I had was, since I am on a multi language environment, I had to change all the reference to $field['und'] to $field['en'] and my content type is in english by default.

inversed’s picture

subscribe

rickmanelius’s picture

subscribe

rickmanelius’s picture

As per #21, this patch needs to be rewritten to comply with the alterered functions in the relations module {#1150004].

So this patch uses
$validate_function = '_node_reference_potential_references';
$matches = $validate_function($info, $v, 'equals', array(), 1);

But references now has the form


 * @param $options
 *   An array of options to limit the scope of the returned list. The following
 *   key/value pairs are accepted:
 *   - string: string to filter titles on (used by autocomplete).
 *   - match: operator to match the above string against, can be any of:
 *     'contains', 'equals', 'starts_with'. Defaults to 'contains'.
 *   - ids: array of specific node ids to lookup.
 *   - limit: maximum size of the the result set. Defaults to 0 (no limit).

function node_reference_potential_references($field, $options = array())

Not a major rewrite, but some changes nonetheless.

rickmanelius’s picture

I've added to the patch in #1 to include the new function format for the references module described in #29.

This ignores the patch in #11 now. I can confirm this works when matched with several other patches.
#973876: NULL $config causing "Unsupported operand..."
#1169986: Fatal error: Class 'FeedsPlugin' not found

I was able to use a feed importer to pull in a node title and a node reference by node title using this patch. Feedback is appreciated.

stwardzi’s picture

subscribe

BlakeLucchesi’s picture

Status: Needs work » Needs review
FileSize
9.77 KB

I took a moment to add support for mapping parent node references as a feed source to the child nodes. This has been tested on just my machine with a few different feeds. Would be great to get a few others to review and make adjustments as needed. This patch also includes the work submitted in #30 to map the targets to the children.

BlakeLucchesi’s picture

I also wanted to mention that I get this notice when importing nodes:

Notice: Undefined offset: 2 in references_feeds_set_target() (line 158 of /Users/blakelucchesi/Sites/xprize/sites/all/modules/feeds/mappers/references.inc).

It has to do with this line in the target mapping code:

// Determine whether we are matching against the title/user or nid/uid, and whether duplicates are allowed.
list($target, $match_key, $duplicates) = explode(':', $target, 3);

It assumes there are 3 parts to the $target string, which isn't true when selecting the target option without duplicates. Certainly a nice one liner of code, but it looks like the assignments need to be split up since you can't count on there being three keys after the explode.

rickmanelius’s picture

I'm only worried that we've put up so many versions of this patch that the maintainer won't have time to iterate through without review by the community.

Is it possible to review #30 or #32 and get the merged? Then we can add more features on top with futher testing from the community in the dev branch...

dotman’s picture

Issue tags: +feeds user reference

Could someone please tell me what is the best patch(s) to apply for user and node reference? I'm in particular need of user at the moment. And should the field widget be set to list and not auto complete, or doesn't that matter. Thanks.

Micha1111’s picture

I have tested patch #32

After following changes in function references_feeds_set_target($source, $entity, $target, $value) {
I can import nodereferences by given nid's in csv files:

delete pre underscores in validate function names
_user_reference_potential_references => user_reference_potential_references
and
_node_reference_potential_references => node_reference_potential_references

$matches = $validate_function($info, '', NULL, array($options['string'])); has too many parameters, because $options includes all 4 needed parameters (see #29)
$matches = $validate_function($info, array($options)); works for me

00trav’s picture

Assigned: Nephele » 00trav

I tried the patch in#32 with modification from #36.

It seems to always insert the first available node id and user id from the available nodes or users.

00trav’s picture

I think there is an issue with $options['ids'] = key($matches);
it always spits out the first value of the array. I fixed with adding a manual check against the value passed from the $value array.

Pocketpain’s picture

Tried #1 but without result. Surely I'm supposed to get the nodereference in the mapping dropdown list?

dotman’s picture

Hey funkyrandy, I think i'm trying to accomplish what you've done. I can have a user reference filed set to unlimited and a csv with the userid's (although i'd prefer using usernames, but that throws an error) in a comma separated field. it will map the correct id to the author is there is only on id, but if i have more than one, my "byline" user reference field is empty.

could you tell me is you're able to map multiple authors to a userreference field for a node, and the exact order of patches you applied to get this to work?

oddly, if I use the patch in the very first post, not #1, i get the above (without the duplicate options), but if i start with the patch in #1, i get ajax errors.

any advice on getting multiple authors mapped to my user reference field would be much appreciated.

Thanks.

dotman’s picture

sorry, i'm at my wits end. can anyone tell me the right patch(s) to use to simply add nodes to a content type that has a user field reference set to multiple/select list pulling data from a csv that has a comma separated list of usid's? I would be in your debt.

Edit: Ok, I've applied patch #32 and#36 and now my field "byline" set to a user reference field is always set to admin, no matter what userid's i put in the csv. If I chang the mapper to User name, I get "no nodes were imported".

Why won't the user reference field work correctly? is anyone getting it to map to a comma separated filed using either userid's or usernames?

Thanks.

TimelessDomain’s picture

Issue tags: +feeds mapper

applied #32, tested, then applied #36 (tried each change individually then together - removing underscores & changing $matches function)

Sources now has "Feed node: Node Reference (nid): field_name" (nid means i should map it by node ID instead of node title, right?)
which was mapped to "Field (Node reference by node ID)"

also tried
Field (Node reference by node title) -- allow duplicate nodes
Field (Node reference by node ID) -- allow duplicate nodes
Field (Node reference by node title)

this did not work though

(note: i switched parser to Common syndication parser temporarily to reveal possible sources. i then copied this text back into the source mappings for the CSV parser. This worked perfectly using taxonomy terms (Feed node: Taxonomy: Taxonomy Name), but its not working for the node reference field. I'm using a multi-valued node reference field, but as of now i just need 1 node reference field mapped, though we should accommodate for multiple values eventually.) Thanks

TimelessDomain’s picture

Status: Needs review » Needs work
dotman’s picture

i updated my references module to the dev (from beta 3), and am using Curent Dev version of Feeds.

When i put in a userid in my csv, it just poulates my node with the first user in the list, so the users are all the same.

Is anyone getting this to happen correctly? With either userid or user name? Multiple values?

Thanks.

dotman’s picture

hey 00trav, could you share the validating code you used to fix the uid from just using the first value in the array instead of the one specified in the csv. Would really help me out.

00trav’s picture

so this is what i got
replace $options['ids'] = key($matches); //(I think this is around line 214, i put a bunch of debug code in mine so the line numbers are off from the patch file)
with this


foreach ($matches as $k => $v){
  if ($options['string'] == $k){
    $options['ids'] = $k;
  }

}

I only tested this with using NID and UID (not name and title). also, my fields are unique, so i didn't test multiple values.

Also, i have no idea if there is proper validation, this is pretty much a total hack just to get it to work. I also noticed another issue, when importing nodes to update, it doesn't update the reference fields.

marcoscano’s picture

hello all

I've tried patch on #32, then I can see the options

Field (Node reference by node title) -- allow duplicate nodes
Field (Node reference by node ID) -- allow duplicate nodes
Field (Node reference by node title)

in the mapping section, but when I attempted to import the CSV file I got the error:

Fatal error: Call to undefined function _node_reference_potential_references() in /.../sites/all/modules/feeds/mappers/references.inc on line 198

In my CSV file I'm trying to import two fields that are node references, and selected "Node reference by node title"

Any suggestion on what I may be doing wrong?

Thanks in advance, I'm quite new to building sites with Drupal, still struggling with some very basic settings...

marcoscano’s picture

sorry everybody, just applied the modification indicated on #36 and now it imports everything, thanks for the patch.

Just having some issues with one field (date), which is not being correctly imported (it's taking current GMT time, instead of the one in the csv file) but that's another issue..

sorry for bothering

Micha1111’s picture

FileSize
49.04 KB

OK, I have tested the code including the changes #46 with feed settings nid and (allow duplicate nodes).
I tried to import 4 records from csv file
1st with correct nid
2nd with the same correct nid
3rd with a different but correct nid
4th with a different and wrong nid by design

3 records were imported
The 4th record validation failed as expected, but two sql error messages are shown

Seems like Feeds module is trying to insert the wrong record in database table, although the validation failed before.
I don't know, which function causes this error.

00trav’s picture

Have you tried dumping the $matches variable (contents returned from $validate_function($info, array($options)) )?

I think the issue might be with the validate function.

dfrishberg’s picture

@Pocketpain re. #39, I apologize if this is a dumb question, but I fell for this problem. Are you sure you're clicking the "Add" button and not the "Save" button?

muhleder’s picture

Here's a patch rolled against dev which builds upon #32, #36, #46. It also includes a fix removing $sources = array(); from

function node_reference_feeds_parser_sources_alter(&$sources, $content_type) {
  $sources = array();
  if (!empty($content_type)) {
    $fields = field_info_fields();

which obviously wipes out any existing sources.

TimelessDomain’s picture

Status: Needs work » Needs review
Pocketpain’s picture

#51: I was adding... but now I worked it out.

00trav’s picture

Tested #52 and it worked (however, I haven't updated to drupal core 7.4 yet). Will retest tonight.

@muhleder - thank you for wrapping the edits into one patch!

Summit’s picture

Subscribing, greetings, Martijn

Summit’s picture

Hi, It also works on Core 7.4 :)
Would it also possible to have term reference added please, see also this forumpost: https://drupal.org/node/1191036 ?

EDIT: Term reference works from patch: http://drupal.org/node/988856#comment-4693684.

Greetings, Martijn

TimelessDomain’s picture

@Summit - term reference already works as per #959984: taxonomy_node_get_terms doesn't work with drupal 7

#54 & #55 look like they are using 2 different solutions.

Can someone please step in & pick the best method, then create a final "combined" patch? we don't want this issue to continue to grow with non-aligned methods otherwise it may become too convoluted (& get delayed further as a result). Thanks

paulgemini’s picture

subbing

Steven.Pescador’s picture

subscribe

muhleder’s picture

@TimelessDomain 'Can someone please step in & pick the best method, then create a final "combined" patch? '

That's what I did in #52. Try the patch, if it works, report that it does. if it doesn't report what bugs you've found.

johnv’s picture

posted wrong file - please continue with #63.

johnv’s picture

This patch is based upon #52 and includes mapping against the fields GUID and URL, as stored in table feeds_item. It was posted before in #11.

vinotwit’s picture

I also had the "Call to undefined function _node_reference_potential_references" fatal error. I have applied patch #52 and it worked for me - 7.4 Core, using an Xpath mapper import for content node creation with node reference on title, not nid. cc @muhleder

Cheers --Mike

Bevan’s picture

Status: Needs review » Reviewed & tested by the community
FileSize
12.61 KB

Looks like this is ready. The attached patch is the same as #63 but actually applies (I got errors with #63) and is a drupal-standard (git) patch.

dboulet’s picture

Patch in #65 works fine, thank you.

dotman’s picture

Does #66 support multiple users for a user reference field by name? that would be awesome if possible, since that's what my csv contains coming from another CMS datasource.

dot.

dotman’s picture

#66 doesn't work at for me with either username or usid when my field is either:

"24, 25"
"Mary Jane, Paul Smith"

These users definitly exist. am I missing something?

dot.

dboulet’s picture

Hi dotman, maybe try splitting those uids into multiple columns and map each to the reference field.

Bevan’s picture

dotman; If you are migrating different types of related objects you will probably find that http://drupal.org/project/migrate better suits your needs. You will need development skills (or a developer) to make use of it, but it handles complex data models for one-time imports (such as site migrations) better than Feeds. Feeds is great for recurring imports from a parallel live data source, but is designed for primarily "flat" data sources; i.e. a list of objects of the same type with a list of attributes.

nyl_auster’s picture

suscribe

jdlind38’s picture

sub

johnv’s picture

Dotman, you need to make feeds know you are passing an array. You can do this e.g via the explode-function of FeedsTamper.

Summit’s picture

Hi, Just used the explode function of Feeds Tamper..it is working great!
Just use another delimeter than the normal feeds delimeter, and you Rock!

greetings, Martijn

SonniesEdge-1’s picture

Tried this out, and CSV files refuse to import when a NID is specified in the mapper. However, there are no errors reported - final message is "xxx nodes imported", but nothing shows up in the content list. Same CSV imports fine if NID mapper is removed.

Happy to test this out further if anyone can offer pointers.

Summit’s picture

Hi, Feeds has problems with numeric values, see this issue: http://drupal.org/node/1019688
This has I think nothing to do with the reference mappers.
For me http://drupal.org/node/1019688#comment-4094658 worked!

Greetings,
Martijn

Bram Esposito’s picture

The version in #65 fails to import multiple references. That is similar to the issue for taxonomy in http://drupal.org/node/1039134#comment-4389556 .

I attach a modified version of #65 that can import multiple references: see documentation for drupal_explode_tags http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_ex...

Bram Esposito’s picture

Sorry, wrong attachment.

Use this one instead:

johnv’s picture

@Bram,
I don't think you should explode the data. This function is a mapper, not a parser.
Your change (If I'm correct) assumes a comma as a separator, which is not always the case.
Multi-values should enter the mapper as an array, not a comma-separated string.

In other issues, maintainer also states that multivalues are not supported. Feeds Tamper does the trick, and you can set your own delimiter there in the UI. See #73, #74 for 2 succesful testimonials.

ShaneOnABike’s picture

+1 Awesome! When is this going to get rolled into the module.

13rac1’s picture

Title: Mapper for node_reference and user_reference fields in Drupal 7 » Feeds integration for node_reference and user_reference fields
Project: Feeds » References
Component: Code » Code: node_reference
Assigned: 00trav » Unassigned
Status: Reviewed & tested by the community » Needs review
FileSize
12.49 KB

I don't want to step on any toes, but this should be added to the References module instead of the Feeds module. It is feeds integration for the reference module and Feeds will soon support the same type of integration as Rules or Views. New file would be named references.feeds.inc. Only problem is a patch is required #1224836: modulename.feeds.inc files are not automatically included, or temporarily a require_once() is needed in references.module. Attached patch uses modified code from #65 for use with the 2011-Aug-02 references git.

johnv’s picture

+1. IMO, moving the issue to References is the right thing to do.
Until #1224836: modulename.feeds.inc files are not automatically included is solved, you must add the following line to references.module:
module_load_include ('inc', 'references', 'references.feeds');

presleyd’s picture

That should read:
module_load_include ('inc', 'references', 'references.feeds');

This patch is a lifesaver for me

presleyd’s picture

Took the file in #81 and added the line for module_load_include into an implementation of hook_init so that it is no longer necessary to add it as per #82 and #83

This patch works until #1224836: modulename.feeds.inc files are not automatically included is resolved. If this issue is resolved use the patch in #81 instead!

johnv’s picture

@preselyd, #83, indeed. I edited in #82.
@presleyd, #84. I normally put the include outside any function. Perhaps your solution is better for memory consumtion?
Anyway, I didn't include the module_load_include()-line in the patch, since the final solution doesn't need this line. IMO it's better implement #1224836: modulename.feeds.inc files are not automatically included FTTB.

presleyd’s picture

@johnv After reading up on the Feeds issue more I agree, the line isn't needed. I'll update my post. PS I don't know about memory but using implementing hook_init seemed more Drupalish..

Swipular’s picture

Subscribe.

jenna.tollerson’s picture

ropaolle’s picture

Another node reference patch can be found here #1271872: Patch: Node_reference mapper D7. It seems to use different code base than the patch here, meybe you should merge your patches.

breckenr’s picture

Sub

13rac1’s picture

@ropalle I've marked #1271872: Patch: Node_reference mapper D7 as duplicate of this issue.

saitanay’s picture

johnbarclay’s picture

I applied #84 and used with node references that were both singular and comma separated. Worked fine for me. Very useful. Thanks.

In the import process, I had to create 2 feeds: one to bring in the initial nodes and a second to update the nodes with references. This is because the node references won't validate if the nodes they reference don't exist yet.

Drupal 7.8, current feeds head, References 7.x-2.0-dev

Georgique’s picture

Sub

ryantollefson’s picture

subscribe

mukhsim’s picture

#84 did not work with multiple items passed as a single comma delimited value.
Attached is a fix for that.

Mukhsim.

Status: Needs review » Needs work

The last submitted patch, references_feed_inc-988856-84_updated.patch, failed testing.

Summit’s picture

Hi,

Latest patch (#96), gives me on references:

Notice: Undefined offset: 2 in references_feeds_set_target() (regel 208 van httpdocs/sites/all/modules/references/references.feeds.inc).

This error is for every node I want to import.
Thanks for helping in advance!
greetings,
Martijn

jack000’s picture

it seems the problem is in isset($options['ids']), which returns true on an empty array. I changed this to !empty($options['ids']) and it worked for me.

Omniweb’s picture

Status: Needs work » Needs review

feeds-mappers-references.patch queued for re-testing.

timothykc’s picture

subscribe

Summit’s picture

Hi,
I think patch #96: http://drupal.org/files/references_feed_inc-988856-84_updated.patch with addon:

isset($options['ids'])  --> !empty($options['ids']) 

Should be queued for re-testing, right?
Greetings, Martijn

presleyd’s picture

Status: Needs work » Needs review
Issue tags: -feeds mapper, -feeds user reference

Some other formatting errors in there I believe

Status: Needs review » Needs work

The last submitted patch, references_feed_inc-988856-84_updated.patch, failed testing.

grasmash’s picture

Fixed syntax errors in patch from #96, added change from #102.

EDIT: please remember to give git attribution to Nephele and other contributors.

cwithout’s picture

Good work on the patch. Just installed it and got references working for user by user name

But all the code is in references.module I mean a .inc file in references module directory. Shouldn't the hooks be in their corresponding modules?

node_reference_feeds_parser_sources_alter in node_reference.module
user_reference_feeds_processor_targets_alter in user_reference.module
Etc.

Edit: Not saying what you have is wrong, but I was under the impression that best practice is for hooks to be kept with their modules. So if it's in an .inc file, there should be separate files for node_reference module and user_reference module that live inside those module's directories. I can see good reason for wanting to keep them all in one file though.

msamavi’s picture

Thanks all so far.
For me patch 105 does not work when the string contains comma character within the node title, further more I tried the node id (the "Node reference by node title" option) with no success!

johnv’s picture

@msamavi , this is since #77, where multiple values are introduced. IMO this does not belong in the mapper, as you experience.
This can be added in your own custom code/modification, or better use Feeds Tamper to do this.
Can you remove the part below and test again?

+  // Handle comma delimited or non-multiple values.
+  if (!is_array($value)) {
+    if (strpos($value, ',')) {
+      $value = explode(',', $value);
+    }
+    else {
+      $value = array($value);
+    }
+  }
Anonymous’s picture

multiple values are interoduced. IMO this does not belong in the mapper

The mapper for Link fileds in Feeds itself does handle multiple valued fields, so I would say this probably does belong in the mapper. However, the handling for comma-separation that is in there should not be included IMO.

johnv’s picture

Title: Feeds integration for node_reference and user_reference fields » Feeds mapper for node_reference and user_reference fields

@linclark,
Indeed, I could be more precies. Multiple values ARE supported by the mapper. Every single value is a record in the $value array. Making sure that a string is split into more then one value should be done in the parser.

sanguis’s picture

thank you just what I needed #105 worked for me

grasmash’s picture

@98 - I was having the same error. I made another small change to the patch, which seems to have fixed it. Attached.

colan’s picture

For those of you that are considering converting, I've got a Feeds patch for Entity reference over at #1341012: Feeds integration for Entity reference fields that could use some testing. ;)

jack000’s picture

$field = isset($entity->$target) ? $entity->$target : array();
  if (!isset($field['und'])) {
   $field['und'] = array();
  }

It seems that with this line I am only able to *append* references rather than set them - is this the desired behavior? The other imported fields overwrite the existing content.

a few more suggestions:
- trim white space on node ids
- suppress notices on empty values (I get hundreds of notices when I import stuff)

rtdean93’s picture

#122 worked great. Thanks for this just in time patch.

elliotttf’s picture

Status: Needs review » Reviewed & tested by the community

Code looks good and is working for me.

You might be able to simplify the code a bit by using a common callback for the node_reference and user_reference targets, but I wouldn't say it's critical.

yched’s picture

Status: Reviewed & tested by the community » Fixed

I know very little about Feeds API, so I'll blindly trust the various people reporting in this thread.

Took a brief look at the code, fixed a couple code style issues, and committed.
Thanks all !

tbeckett’s picture

Title: Feeds mapper for node_reference and user_reference fields » Grrrr...
Version: 7.x-2.x-dev » 7.x-2.0-beta3

The patches no longer work with the latest version of references. Help!

tbeckett’s picture

Category: feature » bug

The latest version of references kills this patch, and viola no import from feeds...

johnv’s picture

Title: Grrrr... » Feeds mapper for node_reference and user_reference fields
Status: Fixed » Active

I understand your frustration, however... resetting title

yched’s picture

The patch has been committed 2 days ago. If you grabbed a version of the code since then, there is no need to apply the patch, the code is already in there.

Now, are you telling me that the current code (that includes the committed patch) doesn't work ?

tbeckett’s picture

Sorry, I didn't realize that it was going live.

tbeckett’s picture

Aha! Clear Caches, and it works perfectly. Yes, today I got to be that NOOB! Thank you guys for making things work, responding so fast, and kicking butt.

yched’s picture

Status: Active » Fixed

:-)

Back to closed, then.

rickmanelius’s picture

Amen! A big thanks to everyone for pushing this through :)

luis_san’s picture

Status: Fixed » Needs review
yched’s picture

Status: Needs review » Fixed

@luis_san - kada... : Why back to 'needs review' ? The patch is now in, there is nothing to review anymore.

tbeckett’s picture

Interesting. The new version seems to have issues with csv's that have comma's in their quote defined strings. EG:

"Sustainability assessment of slash-and-burn and fire-free agriculture in Northeastern Para, Brazil"

Gets truncated after Para.

Any thoughts?

johnv’s picture

Yes, this got in as per #77.
I got tired of promoting Feeds Tamper to parse multiple-values in several mapper-issues..
This should reverse it:

function references_feeds_set_target($source, $entity, $target, $value) {
   if (empty($value)) {
     return;
   }

   // Handle comma delimited or non-multiple values.
   if (!is_array($value)) {
-    if (strpos($value, ',')) {
-      $value = explode(',', $value);
-    }
-    else {
-      $value = array($value);
-    }
+    $value = array($value);
   }
yched’s picture

@johnv: is that a code change that should be committed to references.feeds.inc ?

johnv’s picture

IMO, yes.

BTW, I changed above comment/code to latest version. I still use an old version, so cannot provide a proper patch..

Also,
we can ditch the references.module part, since this issue has been committed on 20-dec-2011:
#1139676: feeds_alter doesn't support hook_module_implements_alter, hook_hook_info and hook_hook_info_alter
#1224836: modulename.feeds.inc files are not automatically included
But now we are getting unscientific...

 --- a/references.module
+++ b/references.module
@@ -20,6 +20,13 @@ function reference_autocomplete_access($entity_type, $bundle, $field_name, $enti
 }
 
 /**
+ * Implements hook_init().
+ */
+function references_init() {
+  module_load_include('inc','references','references.feeds');
+}


yched’s picture

Committed the attached patch as per #129.
I'm not using Feeds myself, nor do I know the API, so I'm blindly committing whatever you guys think is valid...

About references_init() : we can actually not simply remove it and rely on hook_hook_info(), since references.feeds.inc contains hooks implementations on behalf of node_reference and user_reference. Left this part as is for now.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

dhakshinait’s picture

can you tell me how to import multiple node reference by feeds using UI?

johnv’s picture

@dhakshinait,
you can't using Feeds UI.
As long as Feeds does not have an option for specifying the separator You have some options:
-You can use Feeds Tamper module. This allows you to 'tamper' with the data before passing it to Feeds. e.g. explode the comma-separated string into an array.
- You can try some Feeds-issue (can't find it right now) that allows you to have each value in a separate column in your csv/xml.
- modify the code using #16/#17.

dhakshinait’s picture

I have tried Feed tamper module, i choosed "explode' plugin from that module for particular multi node reference field and imported a csv data but it imported first value in that columns For example i tried to import values in column(value1,value2,value3). It imported only "value1" from that column..

johnv’s picture

@dhakshinait , you better check the Feeds issue queue. Your problem/feature request is not particular to the references mapper.
It will happen to any multi-value field.

dhakshinait’s picture

Thanks for you help. i found that I have done a mistake in field setting of node reference field[didnt choose unlimited value ] and now
i can able to import multiple values for node reference using feed tamper module....Once again thanks a lot....

ambientdrup’s picture

I'm also experiencing this issue with trying to import a comma separated list of node titles into a node reference field. I can only import one value but if I have multiple values comma separated it says on the import that the node can not be found (even though the node is there). See my other post in both the References and Feeds issue queues: http://drupal.org/node/1518216

I did try using Feeds Tamper and setting an explode plugin with comma separation. I also tried the patch in #112. Neither seems to have fixed the issue.

-Trevor

ambientdrup’s picture

Please see my additional comment via this issue which is related to #139: http://drupal.org/node/1518216#comment-5839556

-Trevor

ambientdrup’s picture

Ok I've spent a good deal of time debugging this today and I've been able to make it work. I can now import multivalue node references using the References module via Feeds per this:

1) I made sure to install the dev version of the References module

2) I installed the dev version of the Feeds Tamper module

3) I installed the alpha4 stable version of Feeds

4) I determined that I can get the multivalue node reference in using pipes (as opposed to commas) as the separation in both the CSV file and in the Feed Tamper module configuration for the mapping; and then in the Feeds mapper we need to use the | as the separator. I've also determined that this import process using pipes will work via both PC or Mac (I was concerned that maybe the Mac was causing issues on the import).

5) Additionally if you have "" wrapping the array or the values in your CSV it does not work. This still appears to be a bug. But without the "" and just using the pipes works.

So the values in the CSV should be:

node1|node2|node3

I've also determined that the ID column header in the CSV that maps to the GUID needs to be lowercase id and also tweaked to be lowercase in the actual importer mapping. If it’s uppercase it throws an odd SLYK error in the CSV file if you try to open it in Excel and that could be causing issues during the import.

So if you follow those instructions above you should be able to make it work. Pipes seem to work best both in the CSV and via the Feeds Tamper configuration for your mapping.

Hope this helps!

Best-

Trevor

raveman’s picture

Version: 7.x-2.0-beta3 » 7.x-2.x-dev

So, I need to set CSV separator to '|' to work ? Or i need to use | in my field ?

id;field1;field2;field3a|field3b|field3c|field3d;

?

And, do I need to configure feed_tamper to explode field3X ?
I can't get working import with multiple node references :(

Thanx for any help

liquidcms’s picture

Status: Closed (fixed) » Active

nvm..

liquidcms’s picture

Status: Active » Closed (fixed)