I've noticed I can add a preview to an image type but it only pulls data from the settings field name in the fields[] within it.
14 replies
Last updated: Jan 27, 2021
H
Sorry, got another question! I've noticed I can add a
previewto an image type but it only pulls data from the
settingsfield name I have created in the
fields[]within it. This wasn't clear that it is possible in the docs (https://www.sanity.io/docs/image-type ). Can I reference the
imageasset in this preview as it doesn't seem to work?
{ title: 'Image', name: 'image', type: 'image', options: { hotspot: true }, fields: [ { title: 'Settings', name: 'settings', type: 'portfolioGallerySettings', options: { isHighlighted: true }, }, ], preview: { select: { size: 'settings.size', artist: 'settings.artistCredit.title', caseStudy: 'settings.caseStudy.content.title', media: 'image' // <<< This doesn't work. Can I get the parent image asset somehow? }, prepare({ size, artist, caseStudy, media }) { return { title: 'derp' } }, },
Jan 27, 2021, 5:22 PM
A
I'm not on a computer to check, but try
media: 'image.asset'.
Jan 27, 2021, 5:25 PM
H
Hi Geoff! Shows up as
undefinedif I try
image,
image.assetetc. Doesn't show that
previewis even a property of image in the docs so not sure how
settingsis coming though but not the parent type
image
Jan 27, 2021, 5:31 PM
A
I will get on a computer and check.
Jan 27, 2021, 5:31 PM
H
Awesome! Just to point out, I have to use an
imagein an
arrayand not an
object(containing an image and settings) as sanity has a weird way of handling bulk uploading assets into the array.
Jan 27, 2021, 5:36 PM
A
Okay. I’m assuming that’s all one contiguous block of code above. As it’s written, it seems odd that the image doesn’t just display automatically.
Jan 27, 2021, 5:43 PM
A
Ohh… hang on. I re-looked at your code.
Try
Try
image: 'image.asset'in preview.select and then
media: imagein prepare.
Jan 27, 2021, 5:44 PM
H
No joy i'm afraid. I'm also logging out the result in prepare with no joy. Perhaps this is a bug or not implemented? Here is what the above code is nested in:
export default { title: 'Gallery', name: 'portfolioGallery', type: 'object', fields: [ { name: 'items', type: 'array', of: [ IMAGE OBJECT HERE...
Jan 27, 2021, 5:49 PM
A
Do you want the preview to apply to the item in your array or to the greater document?
Jan 27, 2021, 5:51 PM
H
This will be for the item in the array. Should look like this but with the preview image:
Jan 27, 2021, 5:53 PM
A
Okay. I’m trying to visualize your entire schema, but I have something that I think is almost identical and it works like what you’re after.
It might be that I’m doing an object inside the array and you’re not, but here’s what I am using (with a bunch of irrelevant stuff cut out):
It might be that I’m doing an object inside the array and you’re not, but here’s what I am using (with a bunch of irrelevant stuff cut out):
{ type: 'array', name: 'minister', title: 'Minister', of: [{ type: 'object', fields: [ { name: 'member', title: 'Member', type: 'reference', to: [{ type: 'mla' }], }, ], preview: { select: { image: 'member.image.asset', }, prepare({ image }) { return { media: image, }; }, }, }], },
Jan 27, 2021, 6:03 PM
H
Hi Geoff, appreciate your help with this. This does indeed work however in my particular use case I cannot use an
objecttype inside an
array. The reason being that the user needs to be able to drag a load of images/videos into the array and that doesn't seem to work unless the
imageand
filetypes are loose and not nested in something like a object 😔
Jan 27, 2021, 6:35 PM
A
The page you took a screenshot of—if you go to inspect, can you post the JSON for the array?
Jan 27, 2021, 6:38 PM
H
Doing that helped reveal that all I need to use was 'asset'! Turns it is was something so simple...just presumed you would need to reference the field name
image😛 Thanks a lot for this Geoff.
Jan 27, 2021, 6:46 PM
A
Nice! Good to hear. 🙂
Jan 27, 2021, 6:49 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.