SortableGridField many_many sort on non default second column

Hi,

on a website I have DO “Products” and “Categories” in a has_many relation. Now customer likes to have an additional “otherCategories” in a many_many relation to “Product”.

On “CategoriesProductHolderPage” Products should be ordered according to “sortOrder”, whereas on “otherCategoriesProductHolderPage” they should be ordered accordung to “otherSortOrder”.

Ordering should be accomplished by drag and drop in Gridfield (with SortableGridField module).

Is there a way to accomplish this? All examples of sorting many_many relations that I found so far refer to default sorting on ONE “sortOrder” ONLY.

SS 4.3.3 with SortableGridField

You should be able to set up many_many_extrafields for the specific sorting data:
https://docs.silverstripe.org/en/4/developer_guides/model/relations/#automatic-many-many-table

Then you can specify the sort field to use in your gridfield on the otherCategoriesHolderPage

The current has_many relation could still use a sortorder specified on the Product object.

Thanks for the answer, @Tim! I didn’t have the YAML Part of the solution.

When I added the YAML, I still had an error on ordering the Products in the Gridfield. @wmk gave me the hint to look for the error message in the network tab of developer tools (webdeveloper window). There I found reordering occured on the wrong column.

So I changed column name from “sortOrder” to “otherSortOrder” in DO “otherCategories” in public function getCMSFields() into:
$gfConfProdukte = GridFieldConfig_RelationEditor::create(20);
$gfConfProdukte->addComponent(new GridFieldSortableRows(‘otherSortOrder’));

That did the job.

1 Like