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)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Anonymous’s picture

This 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.

alex_b’s picture

Title: Request for User reference mapper » Mapper for CCK User reference
Version: 6.x-1.0-alpha12 » 6.x-1.x-dev

#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.

Anonymous’s picture

FileSize
2.05 KB

I'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

Anonymous’s picture

FileSize
2.34 KB

I 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.

Anonymous’s picture

Status: Active » Needs review
alex_b’s picture

http://drupal.org/coding-standards ;-)

//User reference can have more than one value (1-to-many relationship)
//If that is the case, we expect the values to come in as a comma separated

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...

alex_b’s picture

Status: Needs review » Needs work
Anonymous’s picture

Thanks 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.

Dp-Mtl’s picture

Good patchs and comments!

Inspired of these, I add a feature for "user name", if we have user names in feeds

        $user_id = get_user_id_by_name(trim($user_name));
        $field[$i]['uid'] = $user_id[0];
   

the function is something like:

function get_user_id_by_name($user_name) {
    $uids = array();
    $result = db_query(db_rewrite_sql("SELECT u.uid FROM {users} u WHERE u.name = '%s'"), $user_name);
    while ($user = db_fetch_object($result)) {
      $uids[] = $user->uid;
    }
    return $uids; 
}

Hope help

Renee S’s picture

Status: Needs review » Needs work
FileSize
3.57 KB

I 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.

Renee S’s picture

Status: Needs work » Needs review
mry4n’s picture

Status: Needs work » Needs review

Cool. Someone should roll this into a patch. If I knew how to do that I would. Haven't figured that out yet.
-Mike

mrfelton’s picture

Looking for this for Drupal 7 too. Has anyone forward ported any of the patches in this ticket yet?

sillygwailo’s picture

Patch 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.

col_edinburgh’s picture

there's no code in the above patch

sillygwailo’s picture

Huh, so there isn't. Trying again.

m.stenta’s picture

Patch works great. One small change:

In order to allow multiple usernames or UIDs to be saved, I changed the following code:

<?php
  // Allow for multiple-value fields.
  if (!is_array($value)) {
    $value = array($value);
  }
?>

to:

<?php
  // Explode the values into an array of values delimitted by commas.
  $value = explode(',', $value);
?>

New patch attached, generated off of latest 6.x-1.x-dev branch.

twistor’s picture

#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.

m.stenta’s picture

Ah gotcha. Makes sense.

Well I can +1 the patch in #16. It's working great for me.

twistor’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)