Keep editors in current location when using initialValue template in Sanity Studio

5 replies
Last updated: Sep 18, 2024
Hello! Working from the studio, I wonder if there is a way to keep the editors in the current location when using an initiaValue template. Right now, it always takes the editor away from the list it has been used on.
Let me explain myself, we have in the desk structure an place for users to browse posts by categories, When from a term’s list, they hit the “New” button, we make sure the category is automatically assigned to the new post. But at the same time editors are taken to the “All Posts” area of the CMS, rather than remaining inside the current category of the “Posts by Category” area. I’m adding a video to make myself more clear.
AI Update

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 created
  • context: 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 thread
5 replies
In the
documentList
, have you defined a
schemaType
? I wonder if doing that helps.

https://www.sanity.io/docs/structure-builder-reference#schemaType-f8c40e825eef
It might be the case that when creating the document it doesn’t know in which list it belongs, so it defaults to the first known location in the hierarchy
Yes I have unfortunately. To no avail.
I was able to solve this by adding a
canHandleIntent
method before the intiialValueTemplate.
ah. good to know!

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?