Is there a way to reference a top level field for preview?

6 replies
Last updated: Feb 3, 2021

Is there a way to reference a top level field for preview? I am trying to create an image block, and this is my code:

export default {
  name: 'imageObject',
  title: 'Image',
  type: 'image',
  fields: [
    { name: 'caption',
      type: 'simpleBlockContent',
      title: 'Caption',
      options: {
        isHighlighted: true
      }
    }
  ],
  options: {
    hotspot: true
  },
  preview: {
    select: {
      title: 'caption',
      media: 'imageObject'
    }
  }
}
The media for preview is returning nothing.

Feb 3, 2021, 2:45 PM

If I'm remembering correctly, the preview field is for documents and so you'd define it on documents that use the

imageObject
, not the imageObject itself.

Feb 3, 2021, 3:35 PM

previews also work on non-documents! eg if that type is being used in an array πŸ˜„
this should work:

preview: {
    select: {
      media: 'asset'
    }
  }

Feb 3, 2021, 3:37 PM

(asset is a key created for every image)

Feb 3, 2021, 3:37 PM

user B
that worked! Thanks so much!

Feb 3, 2021, 3:39 PM

Is that in the documentation?

Feb 3, 2021, 3:40 PM

youre welcome πŸ˜ƒ not sure, i think i just found it by trial and error

Feb 3, 2021, 3:42 PM

Sanity.io: Get the most out of your content

Sanity.io is a platform to build websites and applications. It comes with great APIs that let you treat content like data. Give your team exactly what they need to edit and publish their content with the customizable Sanity Studio. Get real-time collaboration out of the box. Sanity.io comes with a hosted datastore for JSON documents, query languages like GROQ and GraphQL, CDNs, on-demand asset transformations, presentation agnostic rich text, plugins, and much more.

Don't compromise on developer experience. Join thousands of developers and trusted companies and power your content with Sanity.io. Free to get started, pay-as-you-go on all plans.