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 – build remarkable experiences at scale

Sanity is a customizable solution that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.