👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Issue with document title not appearing in preview

15 replies
Last updated: Jul 12, 2021
Hello - I added a schema type for 'content' and whenever I 'create new' it shows up as 'untitled' and there seems no way to change the name- any idea what I'm doing wrong?
Jul 12, 2021, 5:53 AM
hey! can you share your content schema?
Jul 12, 2021, 7:04 AM
(from a structured content point of view, I think that “content” is too generic for a document type, but that’s beside the point.)
Jul 12, 2021, 7:04 AM
Sanity tries to pick the most suitable field as a title. If it can’t find any, you need to manually specify the field.
Jul 12, 2021, 7:05 AM
export default {
  name: 'content',
  title: 'Content',
  type: 'document',
  icon,
  fields: [
    {
      name: 'title',
      title: 'Title',
      type: 'string',
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: 'title',
        maxLength: 100,
      },
    },
    {
      name: 'metaTitle',
      title: 'Meta Title',
      type: 'string',
    },
    {
      name: 'metaDesc',
      title: 'Meta Description',
      type: 'string',
    },
    {
      name: 'content',
      title: 'Content',
      type: 'blockContent',
    },
    {
      name: 'dateCreated',
      title: 'Date Created',
      type: 'datetime',
      initialValue: (new Date()).toISOString(),
    },
    {
      name: 'image',
      title: 'Main/social Image',
      type: 'image',
      options: {
        hotspot: true,
      },
    },
    {
      name: 'skills',
      title: 'Skills',
      type: 'array',
      of: [{type: 'reference', to: {type: 'skill'}}],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'grades',
      title: 'Grades',
      type: 'array',
      of: [{type: 'reference', to: {type: 'grade'}}],
      options: {
        layout: 'tags'
      }
    },
    {
      name: 'contentType',
      title: 'Content Type',
      type: 'array',
      of: [{type: 'string'}],
      options: {
        list: [
          {title: 'skill', value: 'skill'},
          {title: 'game', value: 'game'},
        ]
      }
    },
  ],
Jul 12, 2021, 3:55 PM
Interesting. The studio should definitely snag the value of
title
and use it in your preview (i.e., as the title of the document in the list). Are you setting a preview later in your schema or is there anything else in your schema that you didn’t paste here? I was able to get your code working with just a few changes (renaming the document name, removing icon, and setting the reference types to document types I already have in my schema).
Jul 12, 2021, 7:36 PM
I have a preview..
Jul 12, 2021, 7:37 PM
Interesting. The studio should definitely snag the value of
title
and use it in your preview (i.e., as the title of the document in the list). Are you setting a preview later in your schema or is there anything else in your schema that you didn’t paste here? I was able to get your code working with just a few changes (renaming the document name, removing icon, and setting the reference types to document types I already have in my schema).
Jul 12, 2021, 7:36 PM
the preview doesn't work in the ui, not sure where I went wrong... could that be causing the untitled?
Jul 12, 2021, 7:38 PM
preview: {
    select: {
      title: 'title',
      content: 'content',
      date: 'dateCreated',
      main: 'image',
      grades: 'grades',
      skills: 'skills',
    },
    prepare(selection) {
      const gradesStr = selection.grades.filter(Boolean).join(', ')
      const skillsStr = selection.skills.filter(Boolean).join(', ')

      return {
        title: `${selection.title} ${year ? `(${year})` : ''}`,
        content: selection.content,
        date: selection.date,
        skills: selection.skills,
        skillsStr: skillsStr,
        grades: selection.grades,
        gradesStr: gradesStr,
        mainImage: selection.main,
      }
    },
  },
Jul 12, 2021, 7:38 PM
(copied from the movie example... didn't edit that part yet, hence the year)
Jul 12, 2021, 7:39 PM
Do you have a link to that example? I don’t recall seeing a
prepare()
that returns more than a few properties (title, subtitle, description, media, and perhaps one or two I’m missing).
In any case, I suspect it’s an error in your preview causing you to see Untitled (I’m guessing you see
Invalid preview config at the top of your document).
Jul 12, 2021, 7:43 PM
yes
Jul 12, 2021, 7:43 PM
okay, I'll get the preview working and see if that works
Jul 12, 2021, 7:45 PM
ya, that did it
Jul 12, 2021, 7:49 PM
Great! If you face any issues getting the functionality you want in your preview, please follow up.
Jul 12, 2021, 7:50 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?