Entity references (such as node references that link one node to another) break when importing nodes that have been exported via the node_export module. This happens because the module currently does not do any checking or updating to account for the fact that the node IDs of imported content may differ from the node IDs they possessed when they were originally exported.


Sheldon Rampton’s picture

Here's a patch I've written that uses UUIDs to keep entity references from breaking during node import. My path only fixes entity references. Additional patching would be necessary to enable this functionality for the node_reference module.

Sheldon Rampton’s picture

Status: Active » Needs review
olklein’s picture

Thank you for submitting this patch Sheldon. One concern is that $entity_original_uuids only get's populated with the NIDs of the nodes that contain an entity reference field. In the scenario of a referenced entity which doesn't have an entity reference field itself, the import would fail. While saving, the (old) NID of the referenced entity would be missing inside your reference ($entity_original_uuids). Thus the $new_uuid (line 634) would not be populated and the reference won't be updated.

As a fix I populate $entity_original_uuids with all the nodes that are to be imported by moving it outside the if-clause. New Patch is attached.

Sheldon Rampton’s picture

Thank you, olklein, for the fix. I've done some additional work to correct taxonomy term reference imports, which should also use UUIDs rather than taxonomy IDs to prevent term references from breaking.

The uuid_features module provides a way of exporting and importing taxonomy terms using UUIDs, so the node_export module just has to use the UUIDs to match up taxonomy term references in the exported node to the correct references in the imported node.

Here's my updated patch that includes the taxonomy terms fix.

ramper’s picture

Title: Entity references break when importing nodes » Entity references and taxonomy terms break when importing nodes

Thank you Sheldon Rampton for the #4 patch (and also olklein, danielb) . I was getting totally frustrated after trying various methods to move some content, but it looks like I've found something that would work for now. Some nodes seem to cause a problem - especially if they have more than two kinds of file attachment - such as image, video and audio. I chose the "Remote file export, URL" for file export, but in some cases I get the error "The website encountered an unexpected error. Please try again later. " However these are few, and I can live with these for now. The main thing is that I got the taxonomy terms exported right!

zeropx’s picture

Thanks. #4 worked great.

One thing I would like to note that if you are doing an import. Is that you can get some rogue UID's which I ended up with.

My fix was to edit the exported file and remap the UID #'s to ones that do exist or to a temp users ID. I am wondering if this is something that should be addressed in this patch or as a separate patch?

EdgarPE’s picture

Tried #4 patch, looks like it works for us on Entity Refereces. Though, it wasn't a thorough test.

pcambra’s picture

The patches in #4 and below are incompatible with the dependency submodule and actually the fix is simpler if you use it for taking care of references and taxonomies.

Here's a patch that moves the references check to a later stage (after import) so all the nodes are actually present, and also saves the node, which is needed at this point.

citricguy’s picture

Confirming that patch #8 also resolves the 'entity relation' issue as well.

Thank you @danielb, @pcambra and @Sheldon Rampton

mediabounds’s picture

Patch #8 also solved the issue that I was having with node dependencies not being restored after importing nodes.

pcambra’s picture

Status: Needs review » Reviewed & tested by the community

Setting #8 as RTBC as for #9 and #10

Atomox’s picture

Issue summary: View changes

After applying the patch in #8, when reimporting, I'm getting duplicate entries for the entities being referenced. I.E. The referenced entities, which already exist, are being recreated. Has anyone else encountered this issue? These duplicated entities are also the only nodes in the system which are missing uuids, while their original versions (in the system, with lower nids) do have UUIDs.

dooug’s picture

Status: Reviewed & tested by the community » Needs review

I applied the patch from #8 on node_export 7.x-3.x.

Why does it add the $save parameter here which is unused in this function?

+++ b/modules/node_export_dependency/node_export_dependency.module
@@ -140,20 +140,16 @@ function node_export_dependency_load_dependencies(&$nodes, $nid) {
+function node_export_dependency_node_export_after_import_alter($nodes, $format, $save) {
CoffeyMachine’s picture

Status: Needs review » Needs work

I tried both #8 and #4. Neither maintained the entity reference in my case where I am importing both the referencING and referencED nodes at the same time.

Sheldon Rampton’s picture

Issue summary: View changes
JMC’s picture

Patch #8 works for me.

@Atomox, I don't seem to be getting the issue you mentioned but I think it would be possible to get it if you have the default setting for how existing nodes are handled - it defaults to "create a new node". I set mine to "Create a new revision..." and it's working fine.

@CoffeyMachine, I've probably misunderstood your comment, but you mentioned that you are importing both nodes at the same time as if you were marking both to be exported yourself, i.e. rather than just selecting the parent node and allowing the dependency module to find and include the referenced nodes. If that's right, it might be worth trying again with just the parent node(s) selected for export?

vdbemden’s picture

Patches #4 combined with #8 worked using "drush ne-export" on my parents and children nodes !
The nodes contained also taxonomy terms and all is good !
Thanks :)

a.milkovsky’s picture

Patches do not create referenced entities but just copies their ids.
Do I understand it right?

danielb’s picture

Status: Needs work » Closed (fixed)