Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I have some custom content types that have a user reference CCK field. I would like to import data from a CSV field that contains, among other field, the user's name (or email, or other unique identifier).
The mapper would then be able to set the custom node's user references to the correct user using the identifier in the CSV file. (Note that since this is a reference field, it allows for a node to reference more than just one user. I.e. one to many relationship)
Comment | File | Size | Author |
---|---|---|---|
#17 | userreference-mapper-748582-17.patch | 3.86 KB | m.stenta |
#16 | userreference-mapper-748582.patch | 3.87 KB | sillygwailo |
#14 | userreference-mapper-748582.patch | 0 bytes | sillygwailo |
#10 | userreference.inc_.txt | 3.57 KB | Renee S |
#4 | patch.txt | 2.34 KB | Anonymous (not verified) |
Comments
Comment #1
Anonymous (not verified) CreditAttribution: Anonymous commentedThis may be a duplicate of http://drupal.org/node/705560 ?
I'm willing to try writing the mapper myself with a little guidance.
1- Is there already a mapper I can use as a template?
2- User reference allow for multiple values (one-to-many), how does one handle this?
PS I'll search for some myself but any pointers to a tutorial or documentation explaining the basics of creating a mapper (i.e. what functions need to be implemented) would be greatly appreciated.
Comment #2
alex_b CreditAttribution: alex_b commented#1: this is actually not a duplicate, but #705560 is a duplicate of #699176: Mapper for user id and user name. #699176 allows mapping to a node's user id field, but you're asking for mapping to a user reference CCK field, right? This is a separate feature request.
Look at feeds/mappers directory for a couple of examples for CCK field mappers.
Comment #3
Anonymous (not verified) CreditAttribution: Anonymous commentedI've written a mapper for a CCK user reference and I'm attaching the patch (just one new file). It assumes that the data passed in to use for the User mapping is the uid.
I have the following issues however:
1- I'm using a CSV file to test. The file contains CSV data representing a node, with the last column being the uid. The mapper works fine if there is just one value. However I cannot get it to work for multiple values. I've tried writing out the field as "11,12,13" but only the first value (11) is used when creating the reference. How does one pass in multiple values?
2- I'm happy to write some tests for this but I'll need some guidance as I have not done it before and don't know where to start
Comment #4
Anonymous (not verified) CreditAttribution: Anonymous commentedI think I figured out #1. I pass the values in as "1,2,3" and in the mapper I explode them into an array.
I've updated my patch. If this is not the correct way to accept multiple values let me know.
Also I have only tested this using a CSV file as input. Not sure this approach works if the Source is not a CSV file.
Comment #5
Anonymous (not verified) CreditAttribution: Anonymous commentedComment #6
alex_b CreditAttribution: alex_b commentedhttp://drupal.org/coding-standards ;-)
Not sure whether I agree with that... there shouldn't be assumptions like these in a mapper, if we want comma separated lists exploded we ideally support this on the parser level...
Comment #7
alex_b CreditAttribution: alex_b commentedComment #8
Anonymous (not verified) CreditAttribution: Anonymous commentedThanks for looking this over Alex. Will read up on the coding standards and try to implement them.
Could you expand on what you mean by doing this 'on the Parser side'? I see that in issue http://drupal.org/node/759966 you say that this feature is not supported, but in issue http://drupal.org/node/724536 for a node reference mapper, someone posted that the node reference mapper works for 1-many relationships.
There is a comment that says 'Handle multiple value fields' but I don't see any code changes in the Parser, just the mapper, so don't understand how that mapper supports multiple values.
Comment #9
Dp-Mtl CreditAttribution: Dp-Mtl commentedGood patchs and comments!
Inspired of these, I add a feature for "user name", if we have user names in feeds
the function is something like:
Hope help
Comment #10
Renee S CreditAttribution: Renee S commentedI can't take credit for this, but: since there is already a nodereference mapper in this queue #724536: Mapper for nodereference field in Drupal 6 (which has been working great for me), I simply altered that mapper for userreference - the userreference lookup function is helpfully identical to the nodereference one :)
This one gives you the option of choosing username or uid. It seems to work fine for my test import, but could use some community testing.
Comment #11
Renee S CreditAttribution: Renee S commentedComment #12
mry4n CreditAttribution: mry4n commentedCool. Someone should roll this into a patch. If I knew how to do that I would. Haven't figured that out yet.
-Mike
Comment #13
mrfelton CreditAttribution: mrfelton commentedLooking for this for Drupal 7 too. Has anyone forward ported any of the patches in this ticket yet?
Comment #14
sillygwailoPatch based on Reinette's dropped-in file attached.
Briefly tested it doing the following:
1. Created a content type with a User reference field and called it "Author"
2. Created a user called "Richard"
3. Created a feed importer with the node processor making nodes of that content type
4. Mapped the feed "Author name" source to the "Author (User by username)" target.
5. Imported
Since the author name in the RSS feed is also "Richard", it matched with the newly created user.
Comment #15
col_edinburgh CreditAttribution: col_edinburgh commentedthere's no code in the above patch
Comment #16
sillygwailoHuh, so there isn't. Trying again.
Comment #17
m.stentaPatch works great. One small change:
In order to allow multiple usernames or UIDs to be saved, I changed the following code:
to:
New patch attached, generated off of latest 6.x-1.x-dev branch.
Comment #18
twistor CreditAttribution: twistor commented#17, sorry, but we can't have a multiple value separator hard-coded like that. Feeds Tamper provides this functionality though.
Leaving as needs review because the patch in #16 is still valid.
Comment #19
m.stentaAh gotcha. Makes sense.
Well I can +1 the patch in #16. It's working great for me.
Comment #20
twistor CreditAttribution: twistor as a volunteer commented