1

This may be a trivial question, but I haven't been able to find a clear answer how to do this in Drupal 8.

How can I organize nodes in a hierarchy like this?

Node
- child
- child
- - child
- child
Node
- child

I know about Node Hierarchy and several other modules which seem to offer this functionality, but I haven't found anything for D8.

It would also be good if this would work in a way allowing for listing chilren of a given node (like "In this section:", followed by a listing of child nodes.

Still, this seems like such a must-have ability to me, it's possible that Drupal can do it out of the box and I just don't know about it. Am I missing something?

4 Answers 4

3

In Drupal 8 you can do this out of the box. Put a reference field in the parent and target it on the child content type. The label of the field would be "In this section". You can theme the section in a field twig of this specific field, for example field--field-reference.html.twig.

You can nest the entities, but if you reference the same content type and you leave the reference field in the view mode of the child there is a risk of an infinite loop, which will result in a time-out.

2
  • Thank you, this is almost exactly what I wanted - but I have many pages which should display the references. Do I have to input it again and again in every page or is it possible to store a list of references somewhere and then just set the page to display the given list of references?
    – Ondrej
    Commented May 31, 2016 at 12:53
  • 1
    You can use three levels. A parent content type, a list content type, which only needs one reference field, where you put in third level a list of children. This list can be reused in different parents.
    – 4uk4
    Commented May 31, 2016 at 13:17
1

I know this question is 3 years old, but I am surprised that no one yet mentioned the Book module in Drupal Core. I did not compare it to entity_hierarchy, so I cannot recommend one or the other, but do take a look if you're looking for hierarchically structured nodes.

0

you could have two entity reference fields

field_parent

and field_grandparent

Then in your query, fore each node, find all items that have the current node referenced in field parent or field grandparent.

select entity_id as child_id from node__field_parent where field_parent_target_id = :parent_id;

and

select entity_id as grandchild_id from node__field_grandparent where field_grandparent_target_id = :grandparent_id;

When you display them, you could loop through, and display all children, and if they have children, display those children.

by using the two fields and only displaying three levels, you prevent infinite loops. You also might want to add a constraint that you couldn't add a child or grandchild to a node as its parent or grandparent.

Otherwise you could just NOT call the function that displays them recursively.

0
0

There's an official D8 port of node hierarchy ready for review here: https://www.drupal.org/project/entity_hierarchy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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