Anyway to auto create slug hierarchy based on relations? Parent/Child

System Information
  • Strapi Version: v4.7.0

Lets say we have a hierarchy of categories and then products, so for example:
Clothes
– Mens
---- T-Shirts
---- Shows
– Womens
---- T-Shirts
---- Shows

We are entering slugs for each page like this:
/clothes/mens/t-shirts
/clothes/mens/shoes
etc.

Then we use the […slug].tsx file to fetch the pages based on the full path above. We do it like this because we can’t fetch it based on a single slug because it could be the same in different categories. Its specific to parent/child relationship.

The problem is… if we enter say 200 sub-categories/products under /clothes/ then want to rename /clothes/ slug to something else, we have to edit 200 sub-categories/products to change the /clothes/ part of their slug.

Is there any way to auto create slugs based on the parent (related field), then that parents parents, then that parents parent etc? So when we edit /clothes/ then all the children slugs change too? Otherwise its a bit of a nightmare.

We did also wonder if we use single word slugs, not a path, but in the front end, I am not sure how to construct the query to fetch the correct page based on each segment. I dont know how to query to fetch the parent of the parent of the parent etc. So if you land on /clothes/mens/t-shirts, how can you fetch the correct entry from slug t-shirts, which has related page with slug mens, and then that related page has a related page with slug clothes? Bear in mind it could be a deeper or shallower hierarchy.

A bonus would also be able to show child pages indented under the parent in the admin, so we can easily see which ones are children of what parent.

Thanks

Hey long shot here, but did you figure this out? I am currently facing a very similar situation

No I didn’t sorry. I was tempted to try it by nesting up to say 5 related queries in our graphql. Actually I may have tried and faced issues but forgot what now sorry.

Instead we just enter the full URLs as the entry slug as there is no nesting in Strapi, then I think for breadcrumbs we fetch the url without the current page and if that exists the. It’s a parent and link it and just do that for every parent to the root.

Something like that. It was a bit complex but it seems to work. I told the client all the slugs/urls need to be manually entered and need to be the same for the parent.

It’s not a good way at all, not like other systems that allow hierarchical structures.

I had a need for something similar and ended up writing a quite complex (but working!) function.
In my case, it returns the whole hierarchy of the ‘parent’ field of my ‘category’ items.
The nice thing is that it uses a single query to get the full hierarchy of an item.

//How to use ?
const chainItems = await getRelationChain(strapi,itemId,'api::category.category','parent');
//will return the whole hierarchy of items (as a flat ordered array) from the 'parent' field, for categories.
//NB: getRelationChainIds() uses a single DB request to retrieve the chain of IDs.

See this file : STRAPI V5 : Returns the full hierarchical chain of entries for the specified relation field (manyToOne) · GitHub