Iâm trying to move our menu hierarchy/taxonomy into DatoCMS but am struggling with how to best model this in the CMS. For example, if my site is organized like:
Home
Blog Posts
Announcements
Release Notes
Products
Shirts
Snakes
Teapots
Ceramic
Steel
Glass
Whatâs the best way to make a drag-and-drop UI for editors to be able to edit the names and links of each item? I was hoping to find something like Wordpressâs menu editor. Is there anything like that in DatoCMS, or modelable to make it similar?
I know you can drag and drop things around the editor UI tree to reorganize pages and content models. But if I do that there, can I expose the result via API somehow?
I tried this out as a Modular Content Field, but it doesnât support proper nesting. I thought about using two different types of blocks (entries vs section headers) but that would only support one level of nesting.
Maybe a better approach would be to make a JSON field with a plugin that uses something like react-sortable-tree?
Iâll try implementing that as a plugin if there isnât a better built-in way.
Edit: Please ignore this post and go to the next one. I misunderstood âtree-like collectionâ as ânested modelâ, but they are not the same thing. See next post. Sorry!
Edit: My bad, I totally missed the âas a treeâ option! That should work. I tried it out and it seems to work, but itâs still a bit confusing in terms of being able to drag things around in the hierarchy like in the video above. If you wanted to move one node under another, it would take several different operations (editing the child, then the grandchild, then deleting the great-grandchild node, then recreating that same structure in another great-grandparent). Itâs messy.
On the query side, graphQL also doesnât seem to allow recursive lookups, so to get all the levels of nested menus, it requires a crazy query like:
query MyQuery {
allMenus {
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
title
reference {
... on MenuRecord {
id
reference {
... on MenuRecord {
id
}
}
title
}
}
}
}
}
}
}
}
}
}
}
}
}
}
title
}
}
Iâll keep exploring both options (plugin field vs nested models). Thanks for the feedback!
Tree-like models have special children, parent and position fields. You will have to allow for your max depth as I do below by nesting more children.
Side note: I am not sure why the Tree-Like fields arenât in your GQL Explorer screenshot, maybe you need to refresh to see them. But for any Tree-Like model you should have these special fields to work with.
My example:
allGlobalHeaderNavigations(
filter: { parent: { exists: "*" } }
orderBy: position_ASC
) {
title
url
position
children {
url
title
position
children {
title
url
position
}
}
}