Discussing linking embedded documents in Sanity.io schemas
9 replies
Last updated: Nov 15, 2021
M
I have a document of type
Parentthat has a
contentfield that is an array of documents of type
Child(not reference to
Child). I would like to create another field that can choose one
Childfrom the `Parent`'s
contentfield. Is this possible?
Nov 12, 2021, 2:05 PM
Hey User! I'm curious, if the
Childdocuments aren't references, what does this look like in your schema? Re: selecting a
Child, what's the ultimate goal of selecting it?
Nov 12, 2021, 4:48 PM
M
const lesson = { type: 'document', name: 'lesson', title: 'Lesson', fields: [ { type: 'array', name: 'content', title: 'Content', of: [ { type: 'section', } ], }, ], } const module = { type: 'document', name: 'module', title: 'Module', fields: [ { type: 'array', name: 'content', title: 'Content', sortable: true, of: [ { type: 'lesson', }, ], }, ], }
Nov 12, 2021, 5:29 PM
M
user M
simplified, but I want to be able to refer to a lessonthat is in a `module`'s
contentarray
Nov 12, 2021, 5:30 PM
M
It looks like there is no way to do this in vanilla Sanity (please tell me I'm wrong) and I'm looking at custom input components. However, the
propspassed to
React.forwardRefdon't seem to provide a means to access the
parentobject / document (or at least, I can't seem to find a way).
Nov 12, 2021, 5:35 PM
M
My use case is that I want to be able to refer to one
lessonfrom another
lesson. I was doing this with references and a custom filter, which worked when the
lessonwas a separate document and referenced by the module. Now that it is embedded in the module, I need another way of referencing / selecting the
lesson.
Nov 12, 2021, 5:50 PM
M
The reason that we have embedded the `lesson`s inside the
module(and
modulein
course, and
coursein
programme) is that there were simply too many loose documents, and the course creators where having to jump through hoops to manage the documents. Now they all exist within the context of a single
programmecontainer, everything is much simpler - except I still need to link from one
lessonto another.
Nov 12, 2021, 5:53 PM
If you're trying to create references between lessons, I recommend you create them as documents. As you mentioned, you could create a custom input component that queries for information in a field in another document, but this is a really brittle content model. I think you'll find it's easier to maintain if you create them as documents, use dynamic filters in your reference fields and create a custom desk structure for organizing the documents.
However, if you do decide to go the custom input component route, you can access other fields in the document by exporting the component with the
However, if you do decide to go the custom input component route, you can access other fields in the document by exporting the component with the
withDocumentHOC .
Nov 12, 2021, 8:14 PM
M
user M
we've gone back to using references, and whilst I can link (refer) to lessons, I will need solve the organisation of content in the desk structure and programme publishing (all linked documents need to be published too). But that is for another day... :-)Nov 15, 2021, 8:08 AM
Working with Structure Builder can be a bit daunting when you first start working with it, so feel free to come back if you get stuck!
Nov 15, 2021, 6:15 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.