✨Discover storytelling in the AI age with Pixar's Matthew Luhn at Sanity Connect, May 8th—register now

Sanity Media Library - How to Add Images to a Slide Show

1 replies
Last updated: Nov 25, 2021
I'm building a prototype CMS in Sanity for evaluation with our highly-structured metadata and content. Having dealt with very poor media managers in Wordpress and Drupal, I was a little disappointed not to find better media management in Sanity. When dealing with 1000's of images, it's important that a) metadata travels with images (don't need to re-type alt & title tags), b) it's easy to search for images by metadata, c) you can easily update an image with a new version. I tried out https://github.com/aratramba/sanity-plugin-media-library which seems nice and allows me to add custom fields. But ideally I don't want to rely on a third-party plugin if possible. Then I realized I could just make a new content type, "photo", that specifies all the metadata I need along with an image of type "image" (Sanity image type).
export default {
  type: 'document', 
  name: 'photo', 
  title: 'Photo', 
  fields: [
    { type: 'string', name: 'title', title: 'Title' }, 
    { type: 'string', name: 'alt', title: 'Alt' },
    { type: 'string', name: 'artist', title: 'Artist'}, 
    { type: 'string', name: 'movement', title: 'Movement' },
    { type: 'string', name: 'period', title: 'Period' },
    { name: "image", type: "image", title: "Image" }
  ]
}
Took me all of 10 minutes to create the type and add it as a reference to a "carousel" slide show.

  title: 'Slides',
  name: 'slides',
  type: 'array',
  of: [
    {
      type: 'reference',
      to: [{type: 'photo'}],
      name: 'slide',
      title: 'Slide'
    }
  ]
I can now add "photos" to any other content type, update the photo metadata or even upload a new image for that photo, and search for images by the content type fields. Super powerful! Have any of you done something similar? Or do you find that a media library plugin works better? Am I missing some feature for which I should use a plugin instead? (btw I'll probably rename it from "photo" to "artwork".)
Nov 23, 2021, 10:03 PM
Hi! Yes, I also chose this approach for several projects. I also remember asking about it and somebody from Sanity told me it's a good approach. Plus you get a warning when trying to delete an image that is already used somewhere.
Nov 25, 2021, 4:20 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.

Was this answer helpful?