Relation Field - Tree Hierarchy

Hi! I created a Categories model with a Nested Tree trait. I need to be able to select multiple categories in Products model.
I added belongsToMany in Products model:

public $belongsToMany = [
    'categories' => [
        Categories::class,
        'table' => 'my_products_products_categories',
        'order' => 'name'
    ],
];

and field in Product model:

categories:
    label: 'Select categories'
    nameFrom: name
    descriptionFrom: description
    span: auto
    type: partial
    path: $/my/products/controllers/products/_categories_field.php

In the relationship configuration file I added:

categories: 
    label: 'Categories'
    view:
        list: 
            columns:
                name:
                    label: 'Name'
                    type: text
        toolbarButtons: add|remove
    manage:
        list: $/my/products/models/categories/columns.yaml
        form: $/my/products/models/categories/fields.yaml
        showSearch: true

How to display categories list in the tree view?

Adding this in config_relation.yaml doesn’t work:

  structure:
      showTree: true

Hi @Radek ,

Did you add use \October\Rain\Database\Traits\SortableRelation; in your Product model?

Also, in the relation definition, it’s missing the 'pivotSortable' => 'sort_order' (if you have sort_order in your table).

Here is an example:

I don’t want to sort related records. I just want to display them in a tree hierarchy.

Here the tree hierarchy is displayed:
image

But not in popup/modal:
image

do you have NestedTree trait, right? use \October\Rain\Database\Traits\NestedTree;
what is showing on Add new category by default?

Yes, categories model has use \October\Rain\Database\Traits\NestedTree;
By default, the list of selected categories is not in the tree hierarchy:
image

I need to add showTree: true. But this setting doesn’t apply to list in the modal.

categories: 
    label: 'Categories'
    view:
        list: 
            columns:
                name:
                    label: 'Name'
                    type: text
        toolbarButtons: add|remove
        showSearch: true
    manage:
        list: $/my/products/models/categories/columns.yaml
        form: $/my/products/models/categories/fields.yaml
        showSearch: true
    structure:
        showTree: true
        showReorder: false

Hi @Radek ,

You can download the Test-plugin and look how it’s built.