Joint session with Vercel: How to build intelligent storefronts (May 15th)

Disable new document creation button at structure and global level with document.newDocumentOptions

Code examples using document.newDocumentOptions to hide the Create new document button at the structure level and/or disable it in the global create menu.

By Urvashi Bangdel Rai, Herman Wikner & Ash


Hide the Create button at the structure level for all schema types

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		if (creationContext.type === 'structure') {
			return [];
		}
		return prev;
	},
},

Hide the Create button at the structure level for a specific schema type

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (type === 'structure' && schemaType == 'pet') {
			return [];
		}
		return prev;
	},
},

Hide the Create button at the structure level for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'structure' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return [];
		}
		return prev;
	},
},

Disable the global create button for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'global' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return [];
		}
		return prev;
	},
},

Remove specific document type from the global create menu

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (type === 'global') {
			return prev.filter((template) => template.templateId !== 'pet');
		}
		return prev;
	},
},

Remove specific document type from the global create menu for a specific role

document: {
	newDocumentOptions: (prev, { currentUser, creationContext }) => {
		const { type, schemaType } = creationContext;
		if (
			type === 'global' &&
			currentUser?.roles.find((role) => role.name === 'editor')
		) {
			return prev.filter((template) => template.templateId !== 'pet');
		}
		return prev;
	},
},

document.newDocumentOptions enables configuring templates for new documents that can be reactive to contextual information, such as the currently logged-in user.

This article features code examples that show how to use document.newDocumentOptions to hide the Create new document button at the structure level, or to disable it in the global create menu.

You can show or hide the creation option depending on conditional options such as schema type, current user role, structure/global menu, and so on.

Contributors

Urvashi Bangdel Rai

Hello World!

Visit Urvashi Bangdel Rai's profile

Herman Wikner

Frontend Developer at Sanity.io

Sweden

Visit Herman Wikner's profile

Ash

The Web and Stuff.

Visit Ash's profile