Closed (fixed)
Project:
Content Synchronization
Version:
3.0.x-dev
Component:
Code
Priority:
Critical
Category:
Bug report
Assigned:
Reporter:
Created:
16 Apr 2020 at 22:50 UTC
Updated:
12 Nov 2021 at 15:54 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
lobodakyrylo commentedI've found that it's realy infinite loop. I created patch and it works for me.
Comment #3
lobodakyrylo commentedComment #4
blanca.esqueda commentedHi @lobodacyril
Thanks for the patch
I added content reference fields to the basic page and article content types:
You right, it causes an infinite cycle.
The patch fixes the cycle, but the reference is not correct for the first node.
When the first node is being imported let's say the basic page: the field reference on the basic page pointing to the article will be empty, because the article doesn't exist yet.
Then when the article node is imported the field reference pointing to the basic page is correct because the page exists at this point.
--
Note:
If the import is run again, then the article reference on the basic page will be updated because now the article exists too.
This is like the chicken or the egg.. the entity reference has to exist in order to be set ><
Comment #5
guiu.rocafort.ferrerI took a different approach to the patch above, where I use the already existing variable $visited and I store the current entity before calling the recursive function again.
What I found is the following:
Asuming we have an entity A with reference to entity B, and B has a reference to entity A.
Entity A is imported, and the reference to entity B is correct.
Entity B is imported, but the reference to entity A is empty.
I believe that what is happening is that the entity is created, but drupal is not accepting the id value as the referenced entity does not exists yet. As @Blanca.Esquerda says, executing the import a second time solves the problem, as the entity was created during the previous import.
A possible approach I am thinking about, involves creating a new Batch operation type, maybe "update_entityreference", that executes at the end, and fixes the references that have been refused by Drupal due to the entity not existing. I am still not sure how difficult it would be to implement something like that yet but i will try to figure it out.
Comment #6
lobodakyrylo commented#5 You are a genius. I tried to avoid duplicates the same way but it didn't work so I sent duplicates array to the recursion. Your patch works for me. Gracias.
Comment #7
lobodakyrylo commented@Blanca.Esqueda to avoid double loading maybe try to add the entity twice to the dependency list?
Comment #8
blanca.esqueda commented@lobodacyril
The solution will require a bit more than adding the entity twice to the dependency list.
For efficiency, there should be a flag to check if an entity has already been imported and avoid re-importing it along with all its dependencies - except when the entity has a dependency and that dependency points back.
The logic should include when the entity has to be re-added to the queue and if it has been triggered more than once.
Comment #9
lobodakyrylo commentedI tried to recreate my supposition and it works. I add an entity twice to the dependency list to save it again when all the dependency entities are loaded. It works for me and I don't need to synchronize everything twice. Patch attached.
The problem is it will not work if you have references like these: A -> B, B -> C, C -> A, because I'm looking for just the next refereces: A -> B, B -> A.
I defined hasBackReference function and didn't have enough time to solve this issue and I suppose it should be also recursive.
Comment #10
lobodakyrylo commented@Blanca.Esqueda my patch does exactly what you say but not sure that this solution is elegant and it still has a bug with a different type of reference.
Comment #11
guiu.rocafort.ferrerI've been working on a solution to the empty references after the import. This patch develops further the idea of importing the fields involved in the dependency loop, after all the content has been imported.
It is not fully tested, and probably it can be improved further, but it is working ( at least ) in the following conditions:
* A->B, B->A ( Entity A has reference to B, Entity B has reference to A ).
* A->B->C, and C->A (
* A->A ( A has a reference to itself ).
It also has a few issues to address, for now I identify those 2:
* When updating the field reference values, the changed entity attribute gets updated too. This causes the files to be listed as modified all the time.
* If there is a dependency loop, but the entity already exists ( it is a "modify" operation ), the late field update strategy is unnecesary. ( not 100% sure about this one ).
* For now, I use $_SESSION variable to pass variables between Batch Operations. I know this is not an ideal way to to this, and some other method should be used instead.
I'm marking this as needs review, and assigning the issue to me. Can someone review the patch and give some feedback ? I am going to invest some more time on the patch, and refine it further (hopefully ) during the next 2 weeks.
Comment #12
fhebert commented@guiu.rocafort.ferrer I understand your logic but what if there is a required reference, like media uid field referencing an user?
I've tested it won't work. You need to modify table schema at runtime before entity get saved. I'm trying a complementary patch.
Comment #13
mkalkbrennerI don't think that a generic solution will solve all issues. You need to know how these entities handle their circular references themselves.
For example there's a circular reference between commerce_product and commerce_product_variation.
The reference from commerce_product to commerce_product_variation gets updated in a post_save hook of commerce_product_variation. Using this knowledge we could simply ignore the reference from commerce_product to commerce_product_variation.
I think the only robust solution here is to maintain a list of references to be ignored instead of writing complicated magic code.
I started it in this patch.
Comment #15
mkalkbrennerI created a merge request for the patch in #13 and also merged #3114499: Drupal 9 Readiness to get it to work in Drupal 9.1.
Comment #16
guiu.rocafort.ferrerThank you for the information and the merge request @mkalkbrenner, i was not aware of commerce_product and commerce_product_variation having that behaviour. I agree with you, with the idea of the most effective way of dealing with this issue is adding a list of references to be ignored.
I will give a go to your merge request changes.
Comment #17
calte commentedI experienced this issue while working with paragraphs module. I implemented fix from https://www.drupal.org/project/content_sync/issues/3128436#comment-13617612 (https://www.drupal.org/files/issues/2020-05-15/issue-3128436-2.patch)
Relevant sections of my composer.json in case this helps anyone else.
Comment #18
blanca.esqueda commentedComment #19
blanca.esqueda commentedThank you lobodacyril, mkalkbrenner, and guiu.rocafort.ferrer for your help analyzing this issue.
I've updated the resolver, I think it considers all the cases:
* A->B, B->A ( Entity A has reference to B, Entity B has reference to A ).
* A->B->C, and C->A ( There is a circular reference in the Entity trail).
* A->A ( A has a reference to itself ).
* A<-B->C ( entity references its children and its parent - as paragraphs: https://www.drupal.org/project/content_sync/issues/3049969)
If someone has a chance to try/test it, that would be great.
This patch should be on top of 3.0.x-dev, and ideally the same version should be on target and the source sites - so they have the same normalize/denormalize data.
--
Things to consider as part of other issues:
* What to do when a dependency doesn't exist?
Entity(ies) having a missing dependency on its trail shouldn't be imported, or try to import it even whit a missing dependency - if it imports successfully good, and if not show a notice/error about that the missing dependency is a required one. what would be better to do?
* For specific cases of circular references as the one that mkalkbrenner explained of commerce_product and commerce_product_variation, I didn't try this patch with commerce entities yet so in case that there are still issues, it will be better to update a normalizer to not include as dependency something that it is not necessary during the content entity export or to remove the dependency that it is not necessary before the content entity import. Version 3.0.x-dev has some normalizers of entities and fields that I've committed recently that show how dependencies are included, and even there is a patch in this issue that shows how a parent field is included as a dependency for paragraphs https://www.drupal.org/project/content_sync/issues/3049969.
Comment #20
blanca.esqueda commentedoh, here is the patch...
Comment #21
blanca.esqueda commentedProfile module creates a dependency profile <-> user
Patch #20 resolved the dependency loop for the attached issue.
Comment #22
juan.silva commentedComment #24
blanca.esqueda commentedCommitted to 3.0.x-dev
Thank you!