Suppose I have a content type, Folder, with 4 items.
+ MyObject
- Child1
- Child2
- Child3
+ Child4
- Child5
- Child6
Suppose I have another content type (let’s call it Alias). This Alias is mainly a reference to another object but folderish: it can contain a bunch of other aliases. I’m going to use --> to indicate this reference in the following tree representations.(“Reference” is mainly an attribute called “reference” that receives the UID from the target object).
Suppose MyAlias now references my MyObject.
+ MyAlias --> MyObject
- (Nothing)
When referencing to MyObject, MyAlias doesn’t know that MyObject is a Folder, so the internal MyAlias children don’t exist. I need to loop through everyone, and create, manually, an Alias inside MyAlias, that is a reference (having the same structure) to MyObject children. A little tree showing what should happen:
+ MyAlias --> MyObject
- Alias --> Child1
- Alias --> Child2
- Alias --> Child3
+ Alias --> Child4
- Alias --> Child5
- Alias --> Child6
I would like to know the best way to iterate through MyObject items, and create the same structure with another objects, using some kind of loop and using invokeFactory in a subscriber. In the end, I would have BOTH trees existing: one of the actual Folder and children, and another of references to this same Folder and children.
(Summarizing: something like collective.alias, but in a really primitive form, just folders an documents, since I can’t use collective.alias.)
The most elegant and Pythonic solution is to write a recursive generator. Assuming this is a method:
Then
This way, you don’t actually have to wrap your action into a function/callable and there’s no inversion of control.