I have a content-type which can have parent-child relationships to n depth. I am wondering how best to design this in Drupal (D8).

My content type has a PDF file field and a few other descriptor fields. Users can upload a revised version of any PDF creating a new instance of content type. I want to be able to display the relationships between the instances. Although in theory they could reach any depth I anticipate children not exceeding 4 depths from an original parent.

My main concern is how to display these items. I would like a view that displays the parent/child relationships to n depth:

-InstanceA
-InstanceB
--InstanceB1
--InstanceB2
---InstanceB2x
---InstanceB2y
---InstanceB2z
--InstanceB3
---InstanceB3x
-InstanceC
...

From this stack exchange answer I see the Views Tree module might have worked but it does not seem to be porting to D8.

I assume I would need an entity reference field. But would it be best to have a parent reference (limit 1) or child references (unlimited)? What are the advantages of either.

I am not in an urgent rush and can wait for a module that is coming to D8. I have built simple modules in D6 before so capable of limited coding.

Is my approach warm or am I barking up the wrong tree? i.e. Is there a completely different better D8 way?

This is clearly not an unusual situation and must have been considered by lots of people far more experienced than me. However, all my searching brings up unrelated results mostly to do with taxonomy. I feel as if this is such a common need I must be missing something very obvious. So forgive me this question if it is stupid.

Maybe there are existing resources I just need pointing towards. Any advise greatly appreciated.

Thanks
Polly

Comments

steeph’s picture

If you use a reference field, it is easy to display them as a tree, in views as well as in node display. You can choose how the reference field is displayed. But I'm not sure i understand how the children/references are created. Do you have this part already set up?

Polly’s picture

Thanks for your response steeph.

Currently I am using an entity reference field which points towards a parent instance (for my very untechnical users this is easier to understand than a field of unlimited children). However, the site is not yet in use so happy to change this if needed.

Since writing my post above I have experimented further. Using the Views Field View module I can embed a view of children for each parent. However, this only works having one view per generation. If I try to self reference a view as a field in that view I get a recursion warning.

However, you seem to be suggesting a simple solution is available. I do suspect I am making this far more complicated than needed.

Thanks
Polly

steeph’s picture

If I understand you correctly, you don't need any additional module to archive the tree display. I think what you are actually looking for is a way to creat the nodes in the way it is needed. If you use the reference field to add children to a node, you only need to create a display mode for the content type that only displays the name/title and further children, then set the field to display the rendered node in this new display mode. If you don't know how to do this, let me know.

Polly’s picture

Yes you do understand correctly.

So. I changed the reference to unlimited children. I created a view and (using relationships and filters) I filtered out all instances which had no other instances referencing them as children. This left me with a view showing teasers of only instances with no parent. On the content type "Manage Display> teaser" I remove all other fields and only included the child reference set to display teaser. And there I have a view displaying the nodes in parent child hierarchy order. Thank you very much steeph!

Now unfortunately how to theme this into a tree is not immediately apparent to me. Currently I have just a flat view like this:

Parent1
Child1
Grandchild1
Parent2
Child2

What I need is a way to see the relationships, such as:

Parent1
  Child1
    Grandchild1
Parent2
  Child2

If you can point me in the right direction that would be amazing. Either way, thanks for your help. I am on my way!

Thanks again
Polly

steeph’s picture

You should be able to just apply a margin-left on the items. It will be repeated for grandchildren, so they would be further right. If you can post a link to the view, I'll try something and post it here.

Polly’s picture

I'm on localhost so cannot show the view. However, I simply added a CSS entry:

div.view-viewname .field__items { margin-left: 40px; }

Perfect!

You've been a great help. Thank you very much steeph.

Polly