nested_set
Provides a field type to organize arbitrary entities in a hierarchy according to the Nested set model.
This is conceptually very similar to Entity Reference Hierarchy, but takes a different approach to solve the same problem. In contrast to Entity Reference Hierarchy this stores the position of the entities in the nested set as field values, so that no additional database table is needed. It also does not extend core's Entity Reference field, the only data that is stored are the position values.
The benefit of this is that because it solely builds on functionality provided by Drupal core, it integrates tightly with the Entity/Field system so that having multiple hierarchy fields on a single entity or per-translation hierarchies is possible. It is also possible to gather almost all hierarchy-related information in a single entity query. (Some information, for example finding the direct siblings of an element, is more difficult to the nature of the Nested Set Model and the limitations of the Entity Query API.)
The downside is that inserting an element into the tree or reparenting an existing element - which inherently requires updating many elements in the hierarchy - requires saving a potentially large number of entities. This is a lot slower than the two simple database queries that are required by Entity Reference Hierarchy by many orders of magnitude. Also, because this does not build on the Entity reference field it cannot build upon the existing infrastructure of widgets and formatters.
Features
Currently only the field type and a list builder is provided, so while the field can be added to any entity type it is currently only usable with a custom entity. The field will automatically append an element to the right of the nested set and the list builder allows easily organizing the entities in a hierarchy similar to the interface for taxonomy terms.
Note that currently the updating of the position values is not batched so that it will only work for small hierarchies.
Roadmap
- Save entities through a batch
- Make the
parenta proper computed property - Add a
childrencomputed property - Provide a widget so that the field will become usable for any entity type
- Add Views integration