Hide/filter document types in reference field "Create New" option

6 replies
Last updated: Dec 30, 2021
Hi. Thanks for the recent new features. I'm looking at the new
reference
field update with the 'Create New' option and wondered if I can hide/filter what the user can create? If using the Sanity/Shopify example, I wouldn't want to create a product. Could there be an option to pass in doc types to ignore?
AI Update

Yes! You can control which document types appear in the "Create new" option for reference fields using the newDocumentOptions configuration in your Sanity Studio.

While there isn't a built-in disableNew per-type option directly on the reference field, you can use the global newDocumentOptions function in your Studio configuration to filter out specific document types based on context. This function receives information about where the creation is being triggered from, including reference field context.

Here's how you can implement this:

// sanity.config.ts
import {defineConfig} from 'sanity'

export default defineConfig({
  // ... other config
  document: {
    newDocumentOptions: (prev, {creationContext}) => {
      // If creating from a reference field
      if (creationContext.type === 'document' && creationContext.schemaType) {
        // Filter out 'product' type when creating from references
        return prev.filter(template => template.templateId !== 'product')
      }
      return prev
    }
  }
})

For more granular control based on which specific reference field is being used, you can check additional properties in the creationContext:

newDocumentOptions: (prev, {creationContext}) => {
  // Check if we're in a reference field context
  if (creationContext.type === 'document') {
    // You can inspect creationContext to determine the parent document type
    // and filter accordingly
    
    // Example: Remove product from all reference field creations
    return prev.filter(template => template.templateId !== 'product')
  }
  return prev
}

Alternative approach: If you want to completely disable inline creation for specific reference fields, you can use the disableNew option:

{
  name: 'relatedItem',
  type: 'reference',
  to: [{type: 'product'}, {type: 'article'}],
  options: {
    disableNew: true  // Disables "Create new" entirely for this field
  }
}

This approach gives you control over the creation experience while maintaining the flexibility of your reference field configuration. The newDocumentOptions method is particularly powerful because it can respond to the specific context where document creation is triggered.

Show original thread
6 replies
Hi User, currently the only configuration available to hide/filter what can be created in the reference input via the global new-document structure
The caveat here though is those items will no longer be available in the new-document menu in the navbar since the configuration is global.

*Y*ou can filter out the templates that come back in
S.defaultInitialValueTemplateItems()
like so:


import S from '@sanity/base/structure-builder'

const typesToExclude = ['product']

export default S.defaultInitialValueTemplateItems().filter(templateItem => typesToExclude.includes(templateItem.id))
let me know if that helps. otherwise, could you open an issue on our github with the feature request?
Hi User, thanks for looking into this. Unfortunately this method gets rid of the create new button completely at a reference field level. In my use case, I've hidden the create new document button in the nav as it doesn't have a desired affect due to a custom studio structure. Was going to see if there was a quick dirty way using css but maybe its worth opening a feature request unless you know any other way?
Hi User. I know this is far from an ideal solution, but a workaround might be to create an array of references with a max of 1—something like:

{
  name: 'multiReference',
  title: 'Multi Reference',
  type: 'array',
  of: [
    { name: 'existingProductRefs', title: 'Reference to Products', type: 'reference', to: [{type: 'product'}], options: { disableNew: true }},
    { name: 'existingOrNewRefs', title: 'Other References or Create New', type: 'reference', to: [{ type: 'home' }, { type: 'collections' }, { type: 'lookbook' }, { type: 'about' }, { type: 'contact' }]}
  ],
  validation: Rule => Rule.max(1)
},
You’d then have two reference objects—one that permits new references and one that doesn’t—and would specify your document types accordingly. It’s a bit of a hack and would likely require some conditional checks and restructuring where you’re consuming these references on your front end (since it’s now an array of objects rather than a reference object).

In any case, I’ve logged your original post as a feature request and will follow up with you if it gets traction.
Hi User, thanks for taking a look at this. I can use this for now and hopefully a new option could be added to ignore refs when creating the field. Thanks for adding the feature request.
Hi User, thanks for taking a look at this. I can use this for now and hopefully a new option could be added to ignore refs when creating the field. Thanks for adding the feature request.

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.

Was this answer helpful?