Displaying unpublished referenced documents in Sanity's desk structure
5 replies
Last updated: Oct 3, 2024
C
Hello everyone,
I have a question regarding the Desk-Structure and the display of documents in Sanity. I am using a
Here is my current code for the `documentList`:
As soon as the
I have already tried to extend the query to include drafts, but that doesn't seem to work.
Do any of you have an idea how I can solve this problem?
I appreciate any tips!
I have a question regarding the Desk-Structure and the display of documents in Sanity. I am using a
documentListin the desk structure that is supposed to display
productModeldocuments. The problem is that these documents are not displayed if they reference a product document that is not published.
Here is my current code for the `documentList`:
S.listItem() .title('Models') .icon(RocketIcon) .child( S.documentList() .apiVersion('v2023-11-17') .title('Models') .filter( '_type == "productModel" && references(*[_type=="product" && productType == "series"]._id)' ) )
productdocument is published, the corresponding
productModeldocuments appear in the list. Is there a way to display the
productModeldocuments in the documentList even if the referenced
productdocument is not yet published?
I have already tried to extend the query to include drafts, but that doesn't seem to work.
Do any of you have an idea how I can solve this problem?
I appreciate any tips!
Oct 2, 2024, 6:41 PM
T
I would assume its the
Also what happens if you change
._idin the end of the filter that is the problem here, what happens if you remove that?
Also what happens if you change
documentListto
documentTypeList?
Oct 2, 2024, 8:58 PM
A reference field in a document will always contain the published
I also didn’t think that projections inside of filters/the
Note that in this example I’m using the Movie DB database that you can bootstrap via the CLI.
_idof said document (unless you’re using weak references). A draft of a document is just a copy of the published version with the
_idprefixed with
drafts.. This is responsible for the mismatch in behavior you’re getting.
I also didn’t think that projections inside of filters/the
referencesfunction worked! This is how I would approach it:
import {map} from 'rxjs' export const structure = (S, context) => S.list() .title('Content') .items([ S.listItem() .title('Models') .child(() => context.documentStore .listenQuery( `*[_type == 'movie' && productType == 'series']{ 'screening': *[_type == 'screening' && references(string::split(^._id, '.')[1]) || references(^._id)], }.screening[]._id`, ) .pipe( map((ids) => { return S.documentList().title('Models').filter(`_id in $ids`).params({ids}) }), ), ), ])
Oct 2, 2024, 9:21 PM
C
thanks that works 😍
i just had to adjust the condition because otherwise documents of other types would have ended up in the list.
get well soon
i just had to adjust the condition because otherwise documents of other types would have ended up in the list.
.listenQuery( `*[_type == 'movie' && productType == 'series']{'screening': *[_type == 'screening' && references(string::split(^._id, '.')[1]) || _type == 'screening' && references(^._id)], }.screening[]._id`, )
Oct 3, 2024, 6:43 AM
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.