How to Concatenate 2 Values for the Preview Title
Question 1: Concatenating values in preview titles
Absolutely! You can concatenate values in the preview title using the prepare function. Here's how to merge your number with the title:
preview: {
select: {
title: 'title',
number: 'number'
},
prepare({title, number}) {
return {
title: `${number} - ${title}`
}
}
}This will give you exactly what you want: "172 - Il faut que tu ruines tout" appearing in the document list. The prepare function lets you transform any selected fields before display, so you can concatenate, format, or manipulate the values however you need.
You can also add subtitles, media, or other fields:
preview: {
select: {
title: 'title',
number: 'number',
media: 'image'
},
prepare({title, number, media}) {
return {
title: `${number} - ${title}`,
subtitle: 'Episode',
media: media
}
}
}Question 2: Singleton documents
I hear you – the singleton pattern does feel a bit involved! Unfortunately, yes, the manual configuration approach is the standard way in Sanity v3. You need to configure three things in your sanity.config.js:
- Structure builder to create a direct link
- Template filtering to hide it from the "New document" menu
- Document actions to remove duplicate/delete options
However, there's good news: you can use the singleton-tools plugin which handles all this configuration automatically. It's much less convoluted:
npm install @sanity/singleton-toolsThen in your config:
import {singletonTools} from '@sanity/singleton-tools'
export default defineConfig({
// ... other config
plugins: [
singletonTools(),
],
})This plugin wraps up all that boilerplate into a simpler interface. The manual approach gives you more control, but for most use cases, the plugin is the way to go and makes singletons much more straightforward to work with.
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.