👋 Next.js Conf 2024: Come build, party, run, and connect with us! See all events

Discussing filtering options for the 'Create New' reference field in Sanity

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?
Dec 9, 2021, 10:09 AM
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))
Dec 9, 2021, 2:59 PM
let me know if that helps. otherwise, could you open an issue on our github with the feature request?
Dec 9, 2021, 3:00 PM
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?
Dec 10, 2021, 9:10 AM
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.
Dec 29, 2021, 11:19 PM
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.
Dec 30, 2021, 4:06 PM
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.
Dec 30, 2021, 4:06 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?