I need integration for entity reference with Feeds module. I'll try to implement it myself but maybe there's someone smarter than me who could help or has already a solution ? :)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

colan’s picture

colan’s picture

Title: Feeds integration » Feeds integration for Entity reference fields

More specific title.

colan’s picture

Status: Active » Needs review
FileSize
4.12 KB
colan’s picture

Status: Needs review » Needs work

Although passing in the entity ID (to represent the destination entity) works well, is simple and matches the data stored in the database, it poses usability problems. Users won't know the entity IDs until those entities are imported. It would therefore require two import passes, the latter being a reconstruction of the former with entity IDs after they become available. Also, it would be a challenge for users to export the IDs from the DB in an efficient way such that they could be added to the next CSV file.

A colleague of mine came up with a better idea: Use GUIDs in the CSV file instead of entity IDs. The user already knows them, because he/she is assigning them in the first place. As a result, only one CSV file is required. (It will still require two passes if references to entities not yet created are imported before the referred to entities themselves. The second pass will fill in the IDs as an update as there is now a mapping between the GUIDs and the entities.)

It looks like this is already being done over at #988856: Feeds mapper for node_reference and user_reference fields (along with some other options we won't get to just yet), so it shouldn't take me too long to come up with something based on that work.

Also, we don't need to record the target type anymore because of #1319040: Remove "target_type" column from db.

colan’s picture

Status: Needs work » Needs review
FileSize
5.05 KB

This one should do it. I had to destroy the Feeds hash in order for the second pass to work. This is because it wouldn't be updated otherwise; Feeds doesn't re-import stuff it's already seen. This is generally good efficiency, but we can't have it in this case. For details, see the code. I put a lot of comments in there.

I decided to keep the target type set because that other patch hasn't gone through yet.

nguyentran’s picture

Status: Needs review » Needs work

Hello Colan

I have follow the #1 and #3 to apply the patch, i can choose the entity reference field to assign. But after i imported, the node unknow the entity ID to assign.

Thank you.

colan’s picture

Status: Needs work » Needs review

Can you try it again with #5 instead of #3? Don't use #3. It's the old one. #5 is the new one. Thanks!

philipz’s picture

I'll be testing #5 this week and I'll report back as soon as I can! :)

travismiller’s picture

@colan: Not sure what I'm missing. I've applied patch #5 to both beta2 and dev but the node reference field I've created will not list as a target in the mapper. Any ideas? Thanks!

colan’s picture

If you've already got the commit from #1 (if not, do that first: git log to check), try clearing all caches (drush cc all). I don't believe that the .inc file will get included unless the module cache is cleared. You can put some debugging output in the patch code to see if it's actually running. I bet it's not. If it is, we've got bigger problems.

travismiller’s picture

Got it. I had a dsm call in the patch that was not running. Hadn't checked to see when #1 was fixed. After applying the patch in #1, the dsm ran and the field was listed as a target. Thanks for your help!

mrfelton’s picture

Patch in #5 looks to be working for me. Thanks!

colan’s picture

Status: Needs review » Reviewed & tested by the community

Thanks folks!

ajinkya.naik’s picture

@Colan : So, what are we supposed to put in the feed for import for the entity field? I tried the node ID of the destination node, but that did not load anything at all.

I come from a Drupal 6 background - I am not all that comfortable with this whole entity thing yet.

Thanks!

colan’s picture

That's a good question. Sorry if I neglected to explain this earlier.

The target ID should actually be the GUID you selected for the target elsewhere in the CSV file. If the target is defined after the reference, then you need to run the CSV file through twice. The reference is only put in the DB once the target is defined. If you don't want to run it through twice, then make sure to define the targets before any entries that reference them.

BiigNiick’s picture

i'm having trouble getting this to work. pardon my noobness, but i can't get the referenced entities to show up in the mapping dropdown menu in feeds mapper. . . is this not how it's expected to work. any help would be appreciated.
thanks,
- nick

colan’s picture

BiigNiick: In the mapping drop-down, you simply select "Entity reference" for the column you're interested in. That field is then filled in your CSV with the target GUID.

Quarath’s picture

I got the Entity Reference field in the dropdown menu of the Feeds importer mappings and it seems working. But I can't figure out what values should be in the appropriate column for the Entity Reference to work. Any help please?

Status: Reviewed & tested by the community » Needs work

The last submitted patch, let_feeds_import_entity_refs-1341012-5.patch, failed testing.

jlholmes’s picture

I have successfully used this patch (#5) against entity reference beta3. It does trigger the following warning in my site log

Warning: Missing argument 5 for entityreference_feeds_set_target(), called in /Users/jce/Sites/drupal/drupal-7.10/sites/all/modules/feeds/plugins/FeedsProcessor.inc on line 407 and defined in entityreference_feeds_set_target() (line 51 of /Users/jce/Sites/drupal/drupal-7.10/sites/all/modules/entityreference/entityreference.feeds.inc).

colan’s picture

@jlholmes: Maybe try it against the dev branch?

For the record, I have no idea why the test bot can't apply the patch. Anyone have any ideas?

samw’s picture

I was looking at applying this, but was wondering whether #1319040: Remove "target_type" column from db will cause a problem.
Should these lines still remain?

$target_type = $info['settings']['target_type'];

$field[$language][$iterator]['target_type'] = $target_type;

colan’s picture

Status: Needs work » Needs review

@samw: This patch isn't doing the saving, so it shouldn't affect anything even if the other one gets committed. If we drop them now though, the DB table will be inconsistent. Just to keep things clean, however, let's re-roll the the patch for whichever issue gets committed last (so it matches the committed one).

Resetting status as I don't trust the bot in this case.

BiigNiick’s picture

patch #5 doesn't apply cleanly any more because of a change to entityreference.info made in #1343854: Separate "Selection" and "behavior" to different plugins.

otherwise everything works.

BiigNiick’s picture

Status: Needs review » Needs work

oh yeah, needs work to fix patch

jeffschuler’s picture

Status: Needs work » Needs review
FileSize
4.75 KB

Hook implementations weren't getting read when only included via files[]= in the .info file.
Re-rolled and added a hook_init() that includes entityreference.feeds.inc if the feeds module is enabled.

colan’s picture

I suppose we should drop the two $target_type lines now that #1319040: Remove "target_type" column from db is committed?

rooby’s picture

I made a quick hack to the entityreference.feeds.inc file so that it accepts a node title and finds the nid based on that.
By replacing:

<?php
      $entity_id = db_query(
        'SELECT entity_id FROM {feeds_item} WHERE guid = :guid',
        array(':guid' => $value)
      )->fetchField();
?>

with my own custom sql.

It is a quick hack that is specific to my needs but I wonder if a generic solution could be made to do something similar.

Do you thnk such a thing would be feasible or too hard given the potential different types of entities being referenced?

jeffschuler’s picture

Removed those $target_type lines.

--- a/entityreference.feeds.inc
+++ b/entityreference.feeds.inc
@@ -64,7 +64,6 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma
 
   // Get some useful field information.
   $info = field_info_field($target);
-  $target_type = $info['settings']['target_type'];
 
   // Set the language of the field depending on the mapping.
   $language = isset($mapping['language']) ? $mapping['language'] : LANGUAGE_NONE;
@@ -94,7 +93,6 @@ function entityreference_feeds_set_target($source, $entity, $target, $value, $ma
       if ($entity_id) {
 
         // Assign the target type and the target ID.
-        $field[$language][$iterator]['target_type'] = $target_type;
         $field[$language][$iterator]['target_id']   = $entity_id;
       }
       else /* there is no $entity_id, no mapping */ {
colan’s picture

Status: Needs review » Needs work

Sorry to be a perfectionist, but the comment above the second one is now misleading. Now it should say, "Assign the target ID". Thanks for finishing this off!

jeffschuler’s picture

Status: Needs work » Needs review
FileSize
4.61 KB

Good catch; thanks!

-        // Assign the target type and the target ID.
+        // Assign the target ID.
alexmc’s picture

What's the status on this folks? I just found out that the relation module doesn't work with Feeds so I am falling back to Entity Reference.... but I still need to be able to populate my data with lots of csv through Feeds.

Is this patch ready to be committed to the code?

THANKS!

jeffschuler’s picture

@alexmc: The patch in #31 is working for me. Appreciate if you could test it out and report back on how well it worked for you.

mmasotti’s picture

31 works for me but I patched beta5. Sorry if that isn't helpful, I'm new to drupal.

The entityreference field often appears as missing in the importer definition and is hidden from the content type definition until cache clear but I assume that is another issue.

madeby’s picture

When I try to apply patch #31 I get:
Checking patch entityreference.feeds.inc...
Checking patch entityreference.module...
error: entityreference.module: No such file or directory

What am I missing? I use Beta 5. Is that it?

UPDATE: I seem to get it with the latest DEV as well. Any ideas?

flynorc’s picture

i'm new to drupal and am wondering what would be the simplest method to apply this patch.
i tried the tortoisesvn (error - that the folder i chose -> the one with entityreference module or feeds module, i tried both), and with eclipse but i can't seem to make it work. any ideas?

colan’s picture

Assigned: colan » Unassigned

Sorry, I don't have the time to work on this anymore.

Norberto Ostallo’s picture

I have tested #31 and it works perfectly for me.
I hope to see this commited soon since it adds a really useful feature.

nfavrod’s picture

Hi everybody,

I read all the topic twice to finally understand that the reference to be passed in the target is the GUID of the entity.

My problem is that I want to reference an Entity (a node) that is not imported with this feed. It was previously imported with another feed importer.

To be more precise, I got two content types: first is "Artist" and second is "Date of performance". Artist have no entity reference. In date of performance, I have an entity reference to an artist.
I have two feed importers, the first to import artist from CSV (no entity reference field), the second to import the Date of Performance, with mapper Entity Reference where I reference the artist node.

So as the artists are already in the DB, it's not possible to map the entity reference field to the artist node? By NID by example? Or even better by the title of the node?

thanks in advance
nicolas

rooby’s picture

@nfavrod:

I used the patch here and modified it a bit to my own needs, which was to import based on node title.
It only took a very small modification.

I'm not sure how feasible it is to make it generic enough to use something like that in the actual module.

See #28 for more info.

madeby’s picture

Got it patched!

However, not sure what I'm doing wrong here but when I try to make an import I get:

Warning: Missing argument 5 for entityreference_feeds_set_target(), called in /sites/all/modules/feeds/plugins/FeedsProcessor.inc on line 398 and defined in entityreference_feeds_set_target() (line 51 of /sites/all/modules/entityreference/entityreference.feeds.inc).

raystin’s picture

madeby’s picture

The error was that I send the entity id instead of the guid. I made a small change to the patch and it now works like I would like it to.

jeffschuler’s picture

@madeby and @rooby: care to share the changes you made to get things working the way you wanted?
maybe just as inline code if the changes were small enough.

rooby’s picture

In the entityreference_feeds_set_target() function of the entityreference.feeds.inc file there is some sql that gets the entity based on the guid.

It is:

<?php
  // 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();
?>

All I did was change that to get the entity based on some other value that is in my csv file.
So I changed mine to:

<?php
      $entity_id = db_query(
        "SELECT nid FROM {node} WHERE type = 'MYNODETYPE' AND title = :title",
        array(':title' => $value)
      )->fetchField();
?>

This works for my purposes because I am only importing nodes that have a entityreference field to nodes of MYNODETYPE and my csv file contains node titles in the entityreference field column.

You can make this sql find entities based on pretty much anything.

enap’s picture

I kept getting (after patching):
Warning: Missing argument 5 for entityreference_feeds_set_target(), called in /sites/all/modules/feeds/plugins/FeedsProcessor.inc on line 398 and defined in entityreference_feeds_set_target() (line 51 of /sites/all/modules/entityreference/entityreference.feeds.inc).

Was using latest dev of feeds & entityreference.

So I changed L:51:

FROM: 
function entityreference_feeds_set_target($source, $entity, $target, $value, $mapping, $input_format = FALSE) {

TO:
function entityreference_feeds_set_target($source, $entity, $target, $value, $mapping = array(), $input_format = FALSE) {

Which stopped the error appearing. I also changed around L79:

FROM:
// 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();

TO:
$entity_id = $value;

..as I was passing the variable I required (the entity id / vid). Seems to work well, and no errors.

EDIT: Be aware that I'm using a custom (mailhandler command) class to pull a string from an email subject (using regex), doing an entity_load to find the entity with the conditions met (ie: title = 'test'), then obtaining the ID, so I don't expect anything other than correct ID's to be passed. This may be a poor approach, but it works. :)

mmasotti’s picture

Patch 31 seems to work fine for node references but does not appear to work with user references. I have tried feeding GUID, uid, uuid, email and username and the data is always blank. The database target_id field for the entityreference field contains an id but it is not correct and nothing shows up in the UI for the same field.

Anonymous’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc1

@jeffschuler thx for the patch! I confirm, patch #31 working fine on Drupal 7.12, Entity reference 7.x-1.0-rc1. The only thing I can't get working - several values in entityreference field (though in field settings it's set to unlimited number of values). I'm using a semicolon as a delimiter for fields and comma (tried to use it with and without spaces) as a delimiter for field values in CSV. Any ideas on how to add several values in entityreference field? Look forward to your prompt reply, guys, thanks in advance!

jeffschuler’s picture

@chebureque: you'll need to use Feeds Tamper to explode multiple values.

Anonymous’s picture

Version: 7.x-1.x-dev » 7.x-1.0-rc1

@jeffschuler thx again, I'll try it!

update: man, you saved plenty of hours for me, pointing at Feeds Tamper... I'll name my son in your honor!)

colan’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev

New features go into the dev branch.

lunk rat’s picture

Version: 7.x-1.0-rc1 » 7.x-1.x-dev

Patch #31 working partially for me.

I have two entity reference fields on my content type. One field updates just fine via feeds; the other field will not update via feed import, no matter what I try. The fields are nearly identical.

I know the CSV is all good because I imported all of the referenced nodes using the same CSV (but a different importer).

Ideas?

lunk rat’s picture

OK, I get it. My post #52 was because I neglected to understand that you need to target the GUID of the entity you want to reference, not the node title of the entitly you want to reference (unless the node title is your GUID).

That's why I had one field working but the other did not: one was by chance mapped to the GUID of the referenced item. I want to explain this in more detail as it seems that I am not the only one who had trouble grasping this:

If you import node type "Artist" using csv source field "artist_id" as the GUID, and you want to import content type "Album" with an entity reference field field_artist that references the "Artist" nodes, then you need to set up your mapper on "Album" import so that the field_artist is mapped to the CSV source field "artist_id". Bam.

I will report issues, as I am about to import 70,000+ nodes with 100,000+ entity references!

colan’s picture

It looks like we'll have to manage 2 types of references:

  • to an existing entity that's not being brought in through the import.
  • to a new entity that is being brought in through the import.

So maybe there should be two (2) options in the pull-down menu for each of these fields, one with "(GUID)" appended and one with "(Entity ID)" appended so the user can pick whichever he/she wants. This also clears up the documentation as what's needed is explicitly stated.

jeffschuler’s picture

I like this route.

colan’s picture

Status: Needs review » Needs work

In that case, updating status. ;)

rsgracey’s picture

*deleted*

gaele’s picture

As I understand it the patch from #31 expects both referring and referenced entities to be in the same CSV file. This means both entities need to be of the same content type, is that correct?

Anonymous’s picture

I guess not, entities are referenced by GUID you specify in CSV.

vregouby’s picture

+1

ambientdrup’s picture

I've run the patch in #31 and #45 so I can map my imported values using Feeds and match them by Node title into the Entity Ref field I have in one of my content types. These match nodes I've already imported using another feed importer on another content type. This is working but only allowing me to map and link one node from the comma separated list. I tried using commas and pipes. I'm also using Feeds Mapper to add an explode plugin to the field mapping so that it explodes on either pipes or commas. This still does not allow multiple values to be imported. I can only get one value imported.

Now if I try this with the References module (with a generic Node Ref field) it does allow for multiple value imports via pipe explode (using Feeds Tamper). So I know this works with the References field. But I'd like to make this work with the Entity Ref field if possible.

Also, on my Entity Ref field I have it set to use the Autocomplete (Tag style) widget and it's set to allow for multiple values.

Any suggestions?

Here's my code to map based on node title per #45 above:

$entity_id = db_query(
        "SELECT nid FROM {node} WHERE type = 'news' AND title = :title",
        array(':title' => $value)
      )->fetchField();

So basically the issue here is allowing for multiple values on the imports and getting those to work.

Also I did confirm that I can add comma separated values manually using the field on my node.

-Trevor

ambientdrup’s picture

I've also tried tweaking the explode to use pipes, and to use commas separated with a space. So something like:

node1, node2, node3

Also tried:

node1,node2,node3

node1|node2|node3

None of these appear to work via Feeds tamper and Entity Reference and per the patches in #31 and #45.

-Trevor

sea4’s picture

Hi, what is the best way to export the nodes as csv so they can be imported using the feeds. Do you export both the referenced and the referencing nodes together in the same csv? if so how is this best accomplished to maintain the references?

thanks for the insights :-)

ambientdrup’s picture

In my case I've created nodes using one content type (A). Then I've imported data from a CSV that maps into another content type (B). This CSV contains a column that has node titles that reference all the nodes in content type A. I've tweaked the module per #31 and 45 to so that the mapping via Feeds maps by node title. So when I import content type B it creates a reference to the A nodes via the Entity Ref field.

That's just my use case.

Best-

Trevor

sea4’s picture

thanks Trevor!

I am just getting a handle on the process. So can we say...

step 1: install the module
step 2: patch the module from #31

Given the scenario of content type A (eg authors) referencing Content type B (eg books written):

(this is where i am a little on fuzzy on the next steps:)
step 3: export the nodes of content type B as csv
step 4: export the nodes of content type A as csv
step 5: import the csv from 3
step 6: import the csv from 4

Is this remotely accurate? Are the csv generated from feeds or from a db export.

Many thanks again.
Stan

ambientdrup’s picture

FileSize
847 bytes

Ok I've tested this by using patch in #31 and then using Tamper to explode on piped separated values on the Entity Ref field. I'm attaching my sample CSV to show you what I've got. This is working but only importing the last value in the piped list, not both values. So I still cannot import multiple values.

I'm also mapping based on GUID here vs. node title.

Any suggestions?

-Trevor

ambientdrup’s picture

In my case the data was generated from a CSV file from an external db outside of Drupal. I then imported all the data to make the "News article" nodes first. Then I imported the data to create the Scientist nodes. The Scientist nodes need to reference the News articles so this is where I'm using Entity Reference field to create this reference.

But your use case/process would most likely work too.

Best-

Trevor

Shiraz Dindar’s picture

success with #31! thank you!

r2integrated’s picture

#31 works. Thanks =)

rsgracey’s picture

When will #31 make it into the standard releases?

Damien Tournoud’s picture

@colan, @jeffschuler: shall we just commit #31 and move the rest to a follow-up? Your call.

lunk rat’s picture

I have been using #31 to import 1000's of nodes with many references. Works without issue.

colan’s picture

Status: Needs work » Reviewed & tested by the community

@DamZ: Commit it. Let's put the enhancement into another issue. This one has gone on long enough.

ambientdrup’s picture

Has their been any follow up/response to the issue I posted in #66? I can get the patch in #31 to work, however I still can't get multivalues imported. Has anyone done this successfully and can you outline your exact process to make that work? Also, is this the enhancement you're speaking of?

I'd still like to figure out why the Entity Ref field will get one value imported but not a piped or comma separated list.

Thanks!

-Trevor

guile2912’s picture

Regarding the #45 trick, to match references on title, here is a small patch that makes things a bit cleaner : no more hard coded content type so you can have multiple importers on different content types.

      $type = key($info['settings']['handler_settings']['target_bundles']);
            
      $entity_id = db_query(
        'SELECT nid FROM {node} WHERE type = :type AND title = :title',
        array(':type' => $type, ':title' => $value)
      )->fetchField();

Of course the way to go would be to give the choice in the target combobox mapping : "my_node_reference_field:guid", "my_node_reference_field:nid", "my_node_reference_field:title" etc

bradjones1’s picture

@colan, @DamZ - is the patch in #31 still slated to be rolled into -dev in the near term? I don't think I see it in the repo as of yet. Thanks for all your work on this!

jeffschuler’s picture

Damien Tournoud: the follow-up changes are going to be crucial, but it seems like #31 is working for enough folks that it's worth committing. Thanks; let's do it.

giorgio79’s picture

Thanks #45 and #75, I was just looking at importing based on referenced entity title. Will be testing the patch for that.

dan carlson’s picture

I don't see file entityreference-feeds.inc to patch??

guile2912’s picture

Just git apply -v the file in #31 in the modules/entityreference folder, and the entityreference-feeds.inc will appear !

Damien Tournoud’s picture

Status: Reviewed & tested by the community » Fixed

Merged #31 into 7.x-1.x, thanks!

Shiraz Dindar’s picture

hooray!

jrao’s picture

Patch to implement the idea in #54 is available in #1616680: Handle more mapping keys in Feeds integration

Timusan’s picture

Hi guys

Any update on the multivalue import of GUIDS using Feeds Tamper to explode on pipe?

Thanks!

EDIT: Or should we move the Feeds Tamper explode issue to the Feeds Tamper issue queue?

Status: Fixed » Closed (fixed)

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

GaëlG’s picture

Status: Closed (fixed) » Needs review
FileSize
4.02 KB

As discussed in #45 and #75, matching against entity label can be useful. Here's a patch which adds this feature. It should work for node titles, user names, etc.
I also added matching against etid, if someone needs it.

EDIT: oops, I just noticed the comment from jrao. I'll update my patch accordingly and post it there.

GaëlG’s picture

Status: Needs review » Closed (fixed)
MTecknology’s picture

Status: Closed (fixed) » Closed (duplicate)

I'm changing this to closed (duplicate) because the duplicate issue is still open. This particular issue isn't actually solved, but as you said in the other report that is the better place to post for follow up.

ambientdrup’s picture

Ok interestingly the patch seems to be in the version of the module I'm using (stable or dev) but I'm getting this error when trying to run the import:

Warning: Missing argument 5 for entityreference_feeds_set_target(), called in /Applications/MAMP/htdocs/site/sites/all/modules/contrib/feeds/plugins/FeedsProcessor.inc on line 407 and defined in entityreference_feeds_set_target() (line 51 of /Applications/MAMP/htdocs/site/sites/all/modules/contrib/entityreference/entityreference.feeds.inc).

The values of that mapped field are not importing even though I have the corresponding nodes set up on the site. This happens when I use the autocomplete or autocomplete (tagging) widget for the field.
Also what is the status of #84?

-Trevor

tky’s picture

Got exactly the same error code with #89 when I use tagging widget for reference field.
With the dev module.

rnield’s picture

I've been having issues getting this patch to work like I wanted, but in the process I have found a crazy work around that I suspect will work for many of you.

Feeds allows you to import into a "node reference" field from the module Reference. That may solve your connection problem, but for me I still needed the link to be an "entity reference". Using the Rules module I setup a trigger when a node is created during import. At that point I used the "Set a Data Value" to copy the reference from "node reference field" to "entity reference field". Yes technically you have 2 connections to the same node, but it is much better then having no connections.

Obviously it is a little ugly, but it works like a champ.

aeava’s picture

Thank you rnield for your suggestion.

I had forgotten how powerful rules can be. Set up the rule and worked flawlessly. I much rather deal with rules than patches. In my case, a couple of thousand nodes being imported was only delayed by 10 seconds or so by having the rule.

thanks again.-

EVENT: Before saving an item imported via [NAME OF FEEDS IMPORT].
ACTIONS: Set a data value -> Parameter: Data: [node:NODE-REFERNCE-FIELD], Value: [node:ENTITY-REFERENCE-FIELD]

argo27’s picture

Hi rnield.

I want to try this solution can you explain further please.

Im trying to use rules between importing data using feeds and entity reference.

I have a content type (ContentTYpe1) that has a field with entity reference. This field is a list field. Displaying list of node titles from another content type (ContentType2).

So when the user key-in the data to ContentTYpe1 he is prompted with an option to select from the list.

e.g Option A | Option B | Option C

Now I will be importing several node data to ContentTYpe1 using feeds and csv.

How can I import the proper selected item from the list in ContentTYpe1?

guile2912’s picture

I guess you could give a try to this patch #1797116: Handle even more mapping keys in Feeds integration so that the value of the list field in contentType1 matches against the title of contentType2

epineda’s picture

You should give a try to Feed Import module. It's a very straight line approach. You prepare your data in an XML file (ton's of options to do so) and after that you will map each Xpath of your XML file with any field in Drupal, doesn't matter if that is a "simple text field" or an "Entity reference" one.

A few clicks and your are done, all your info is imported in Drupal.

Amazing and it works like a charm!

HOW TO:

Here you will find an example of how to use it. Hope this helps another newbie like myself.

http://drupal.org/node/1360374

dankoB’s picture

I hope I'm posting this question to the right place. I'm trying to understand the functionality of Feeds with Entity Reference (vs Feed Import or Migrate) and wonder if it is capable of suiting my need.

I have a D7 site with parent<>student(child), event<>student(participant), and club<>student(member) relationships. These are each separate entity types with Entity Reference (and Corresponding Entity References) connecting them. I would like to take Excel or CSV files of data from events (with student participant, club, and parent info) and import/merge/update this information into the preexisting database. If a student is already in the site database I'd like the student's data to be updated with information about having participated in the tournament, current club, etc., and only if they are not, then to add a new student entry. Similarly I'd like to create new entries for missing referenced entities such as a parent that isn't in the database yet (though I could possibly get around that by doing two separate imports, starting with the parent list).

Is this what Feeds can do? Or would Feed Import or Migrate be more appropriate? It seems for the most part that these each are meant to create new entries rather than searching for and updating ones preexisting in my database (unless created specifically by a previous import). Is this correct?

emjayess’s picture

just arrived here attempting to solve a problem with this particular use case (importing entity reference fields), and wondering if anyone has sized up the effort here as compared to http://drupal.org/project/feeds_entityreference, which at first glance aims to solve the same challenge.

asgar.ahmed’s picture

Status: Closed (duplicate) » Needs review
kingswoodute’s picture

Hey I just wanted to say thanks so much rnield and aeava.

That rule (#91 and #92) is brilliant. Thanks very much for posting it.

haydeniv’s picture

Status: Needs review » Closed (fixed)

Back to fixed.

basvredeling’s picture

For me http://drupal.org/project/feeds_entityreference works better than the rules solution suggested in #91

Masterfool’s picture

Sorry to resurrect this, but entity reference fields are not being uploaded from my CSV with feeds. I have not applied any patches, since I got the impression that the patches here had been included in entity reference.

If I want to upload entity references in a CSV, referring to entities already present on my drupal, what steps do I have to take? All my other fields are feeding in fine.

(I have been perusing this and other issues, as well as the two entityreference - feeds modules, neither of which worked for me.)

Masterfool’s picture

Updated: I followed the instructions outlined in #45, and it worked for me! It just took a re-read to find the right course of action.

Thanks for this patch!

Lumis’s picture

If one just imports nodes ids into the entity reference field then in the entityreference.feeds.inc one just needs to simplify the if statement:

 if ($value) {

	 $field[$language][$iterator]['target_id']   = $value;
         /* everything else should be commented */
}

If the value is a number which does not relate to anything, the entity reference field will just show "-none -".

In theory, the entity reference feed mapping, should ask whether one imports node ids of the referenced table, or data which by comparison should retrieve the referenced ids. If it is the latter, then it should offer a choice of fields from the referenced table to compare with.

Lumis’s picture

Issue summary: View changes

Question change.

ranavaibhav’s picture

Version: 7.x-1.x-dev » 7.x-1.1
Component: Code » Documentation
Category: Feature request » Support request
Issue summary: View changes
Status: Closed (fixed) » Active

Guys, sorry for making this active again. But I read through all the responses above and not having luck in importing data.

I have two content types: (1) Asset (2) Service Contract. In Asset content type, i have a Entity Reference field. I already have some Service Contract nodes created manually (not imported through Feeds).

I need to import Asset nodes - so my questions is that do I enter Service Contract NID in my CSV or something else? There are few mentions of GUID, where do I find this?

Any help is greatly appreciated.

Thank you!!!

lunk rat’s picture

@ranavaibhav the references to GUID are talking about nodes that have been created with feeds and thus have a feeds GUID. Your Service Contract nodes will not have a feeds GUID since they were not created by feeds.

IF they did have a guid, you would use that for your target reference column in the CSV that contains your Asset node feeds import.

I have never successfully imported node referenced using feeds unless I import the references by guid. Maybe someone else can chime in on this?

One trick might be to use views bulk operations to set a guid on your existing nodes (like copy some unique field into the guid field) and then import your references against those.

Hope this helps.

ranavaibhav’s picture

Thanks "lunk_rat". I ended up uploaded uploading my Service Contract nodes through Feeds ; it duplicated all the Service Contracts since Feeds did not recognize my previous nodes. At least for now, I can upload Assets using Service Contract GUID reference.

But still the design of Feeds modules should change because not every content of the website is uploaded through Feeds, and specially when you have website with thousands of nodes existing. In my specific example, I have vendors creating Service Contracts for us manually and my company employees create Asset as we buy them. So Feeds module become unusable.

I wish I would be able to share any patch but I do not know programming. But I believe that Feeds should use existing ID like Entity ID for referencing users/taxonomy/nodes, etc. As someone above mentioned that Entity ID may not be know to end users so in that case there should be an option to reference entity by either of its title, entity ID or any other related information.

It seems from the following issue that entity ID or title is allowed from the patch introduced in February 2013 however it does not seem to work: https://drupal.org/node/1616680

Thanks.

Leeteq’s picture

Version: 7.x-1.1 » 7.x-1.x-dev
Component: Documentation » Code
Category: Support request » Feature request
Status: Active » Closed (fixed)
Anas_maw’s picture

Status: Closed (fixed) » Needs work

There is a small bug in this feature. if you have multiple importers on multiple content types and the GUID is unique per importer not on all importers, if we import using (Entity reference by Feeds GUID), maybe it will return a wrong entity id for another importer if they have same GUID.

rcodina’s picture

@Anas_maw Please, check this out: https://www.drupal.org/node/1394320#comment-10500790

If you have the same issue explained there, please close this issue. Thanks!

Anas_maw’s picture

@rcodina, no i think it's another issue.

in entityreference.feeds.inc file.

line 119

$entity_id = db_select('feeds_item', 'fi')
->fields('fi', array('entity_id'))
->condition($match_key, $value,'=')
->execute()
->fetchField();

the condition of query depend on guid without checking on importer id which is the type of importer.

Maybe it's a feeds issue not this integration.

rcodina’s picture

Maybe it's a feeds issue not this integration.

@Anas_maw This is what I guess but I'm not sure. I think It would be better to open up a new issue that points to this one and then close it. Notice the comment just above your first post was from one year ago...

Anas_maw’s picture

Status: Needs work » Closed (works as designed)
ar-jan’s picture

Status: Closed (works as designed) » Closed (fixed)

Changed to previous state (original issue was fixed).

schnydszch’s picture

Apologies for ressurrecting the thread for this. I'm on Drupal 8 and is trying to import csv into a content type that has entity references. Are the patches from here still holds true for Drupal 8? Or this is already present in stock Drupal? Please advise. Thanks!