I'm struggling to think of a way to create a family tree hierarchy in Craft.

I've tried using categories and structures, but run into the problem of needing child nodes to have two parent nodes.


enter image description here

Generally, what's the best way of achieving this?

  • One child can always have only one parent in a tree hierarchy. Why don't you use a structured section and relate your two "parents" with the real parent for that. Isn't that enough for you? Or you can just use a single and relate your parents with the entry. Its just not so obvious in the list – Robin Schambach Mar 2 '18 at 5:51
  • Hi Robin, thanks for your comment. My diagram wasn't clear in explaining what I'm after. I've updated it. – Adam George Mar 2 '18 at 9:01
  • How about creating a structure per generation, and then using an entry-picker-field for defining the relations? Ie. Parent1 and Parent2 are both entries in the "Parents"-section, Child is a entry in the "Children"-section and through a field on the Child-entry you pick two parents and so forth and so on? – Fuglsetrampen Mar 2 '18 at 9:14

I can't think of an easy and clean way to visualize this kind of structure in a default list. So if you want to be able to display the structure from your diagram dynamically you'll have much work to calculate the positions correctly in a clear way.

So I would suggest you to use easy Craft methods. You could create an entries field to relate your children with all the upper level parents, in your case you would have

Parent 1 - no relation
Parent 2 - no relation
    Child - related to [Parent 1, Parent 2]
        Grand Child - related To [ Child ]
    Sub Parent 1 - related to [Parent 1, Parent 2]
    Sub Parent 2 - related to [Parent 1, Parent 2]
         Grand Child 1 - related To [Sub Parent 1, Sub Parent 2]
         Grand Child 2 - related To [Sub Parent 1, Sub Parent 2]
         Grand Child 3 - related To [Sub Parent 1, Sub Parent 2]

So you can get each parent of an entry with entry.fieldHandle.find() and you can iterate through the tree. But this will only work well when don't mix trees.. So when you have a Parent 3 and 4 with another tree like in the diagram and one of those entries can be the parent of one of the grand children in the first tree you could have trouble (performance wise) to iterate to the beginning. You can collect all parents with no problem but if you have 5+ levels you'll need many queries and it could take a lot of time in Craft2 with default ElementCriteriaModels. But that's always the case in those NxN tree connections

I don't see any issues as long as your trees are separated from each other and as long as you use custom code to fetch the entries.

  • "that's always the case in those NxN tree connections" -- this is what I am realising. Thanks Robin. – Adam George Mar 2 '18 at 10:21

Create a regular channel where the entries have a relationship to the parent and not the child.

That means that each child can have any number of parents and each parent can be related to as many children as needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.