Using referenced document preview values in parent object preview
Yes, you can definitely use preview values from referenced documents in your parent object's preview! The key is using dot notation in the select object to access fields from the referenced document.
Here's how you can set up your array of objects to show the referenced document's preview data:
{
name: 'myArray',
type: 'array',
of: [{
type: 'object',
fields: [
{
name: 'reference',
type: 'reference',
to: [{type: 'yourDocumentType'}]
},
{
name: 'isActive',
type: 'boolean'
}
],
preview: {
select: {
// Access fields from the referenced document using dot notation
title: 'reference.title',
media: 'reference.image',
subtitle: 'reference.subtitle',
isActive: 'isActive'
},
prepare({title, media, subtitle, isActive}) {
return {
title: title,
subtitle: isActive ? `✓ ${subtitle || ''}` : subtitle,
media: media
}
}
}
}]
}The important part is using the dot notation like 'reference.title' in your select object. This pulls data directly from the referenced document. You can access any field from the referenced document this way, including its media/icon.
To get the icon from the referenced document type, you can reference the image field that's used in that document's preview. If the referenced document type has a custom icon defined in its schema, that won't automatically carry over, but you can conditionally set icons in your prepare function based on the reference type.
For your specific use case where you want to preserve the type-specific icons, you might also consider importing the icon from the referenced schema and using it as a fallback:
import { YourDocumentIcon } from './path-to-schema'
prepare({title, media, isActive}) {
return {
title: title,
subtitle: isActive ? '✓ Active' : 'Inactive',
media: media ?? YourDocumentIcon
}
}This approach gives you the same visual experience you had with direct references while maintaining your additional boolean field. You can read more about this in the preview configuration documentation and this guide on creating richer array item previews.
Show original thread8 replies
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.