Contentful logo

Contentful Community

Modeling a nested relationship

I am playing around with Contentful, and seeing if it meets our needs to take some items out of developers hands to manage updates. I have run into something that I don’t quite know how to model. I have a collections of Resources, Categories, and Sections. Each Section is related to a Category, and each Resource is related to a Category and possibly a Section from that Category.

What I can’t figure out how to do is set it up so that during the creation of a resource the editor picks a category and then can pick from a section within that category only. Is there some trick to modeling that scenario?

Hi @matthewalaforest! Welcome!

You can achieve this with a UI extension as the Contentful platform doesn’t natively support this kind of conditional validations. I’ve made a nested category browser extension that could be modified to achieve this.

Alternatively, if you want to say that when an editor picks a section that the category is also selected (i.e., not optionally selected) then you could model this directly: The Resource has a multi-reference field to Section entries and no category field. The Section entry then has a single reference field to Category entries. Thus when a resource has a link to a section there is an indirect link to a category.

Hope this helps!