Controlling entity titles in Sanity.io and customizing their behavior.

5 replies
Last updated: Apr 19, 2021
Hi everyone! Short question: how do you control entities titles? For example, I want all the rows to have a “row 1”, “row 2" etc titles. If there is no title, entities have strange names based on values. Where to read about this? Would really appreciate your help!
Apr 18, 2021, 9:51 PM
Sanity tries to infer a name based on your schema, you can customize the behavior with this config: https://www.sanity.io/docs/previews-list-views#770fd57a8f95
Apr 19, 2021, 5:20 AM
Hi Vlad. I’d definitely recommend taking a look at the link Derek posted. There’s a lot of power in what data you can return as a title, subtitle, description, etc.
You didn’t mention your reasons for not having a title on your document, but if you don’t want to add a visible title in the studio, you could take advantage of the string’s
hidden property as well as an asynchronous initialValue function to query your document count. If your document is named “mainPage,” you could do something like:

import client from 'part:@sanity/base/client';

export default {
  name: 'mainPage',
  type: 'document',
  title: 'Main page row',
  fields: [
    ..., // Your other [boolean] fields
    {
      name: 'title',
      type: 'string',
      hidden: true,
    }
  ],
  initialValue: async () => ({
    title: "row " + await client.fetch(`
      count(*[_type == "mainPage"]) + 1
    `)
  })
}
This would produce a title named
row #
based on the number of
mainPage
documents, but would hide it in the studio.
Apr 19, 2021, 6:27 AM
However, if you were ever to delete a document the next one produced would probably have a duplicate title, and since the title is hidden you’d need to take out-of-studio steps to fix it. That, or better handling of incrementing your row count.
Apr 19, 2021, 6:29 AM
Hi Vlad. I’d definitely recommend taking a look at the link Derek posted. There’s a lot of power in what data you can return as a title, subtitle, description, etc.
You didn’t mention your reasons for not having a title on your document, but if you don’t want to add a visible title in the studio, you could take advantage of the string’s
hidden property as well as an asynchronous initialValue function to query your document count. If your document is named “mainPage,” you could do something like:

import client from 'part:@sanity/base/client';

export default {
  name: 'mainPage',
  type: 'document',
  title: 'Main page row',
  fields: [
    ..., // Your other [boolean] fields
    {
      name: 'title',
      type: 'string',
      hidden: true,
    }
  ],
  initialValue: async () => ({
    title: "row " + await client.fetch(`
      count(*[_type == "mainPage"]) + 1
    `)
  })
}
This would produce a title named
row #
based on the number of
mainPage
documents, but would hide it in the studio.
Apr 19, 2021, 6:27 AM
user G
,
user A
, thank you so much, that’s exactly what I was trying to achieve.
Apr 19, 2021, 9:17 AM

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?