Keep editors in current location when using initialValue template in Sanity Studio
Perfect! This is exactly what you need. The canHandleIntent function is the solution to keeping editors in their current location when creating documents from an initial value template.
When you use an initial value template with the "New" button in a custom desk structure pane, you need to tell that pane to "claim" the intent to create the document. Otherwise, Studio defaults to routing the user to the main document list (your "All Posts" area).
Here's how to implement it in your desk structure:
S.listItem()
.title('Posts by Category')
.child(
S.list()
.title('Categories')
.items([
S.listItem()
.title('Technology')
.child(
S.documentList()
.title('Technology Posts')
.filter('_type == "post" && category._ref == $categoryId')
.params({categoryId: 'technology-category-id'})
.canHandleIntent((intentName, params) => {
// Handle the 'create' intent for posts
return intentName === 'create' && params.type === 'post'
})
)
])
)The key part is the .canHandleIntent() function which receives:
intentName: The type of action (usually'create'or'edit')params: Contains information like the document type being createdcontext: Additional contextual information
By returning true when the intent matches your document type, you're telling Studio "this pane can handle creating this document, keep the user here." This works seamlessly with your initial value templates that pre-populate the category field.
So when an editor clicks "New" from within the "Technology" category list, they'll stay in that filtered view rather than being redirected to "All Posts," and the new post will already have the Technology category assigned via your initial value template.
Show original thread5 replies
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.