👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

Troubleshooting a plugin that disables a document type in Sanity.io

11 replies
Last updated: Feb 21, 2023
I'm trying to extract config logic out into a plugin but doesn't seem to work. The plugin disables a document type from being viewed in the "new document" menu (its an example in the docs). I'll post code in the thread.``````
Feb 21, 2023, 6:59 PM
sanity.config.ts
export default defineConfig({
  basePath: '/studio',
  projectId: projectId,
  dataset: dataset,
  title,
  schema: {
    types: [
      home,
    ],
  },

  document: {
    // logic for plugin here
    newDocumentOptions: (prev, { creationContext }) => {
      if (creationContext.type === 'global') {
        return prev.filter(
          (templateItem) => ![home.title].includes(templateItem.title)
        )
      }
      return prev
    },
    //
  },
  plugins: [
    // singletonPlugin([home.name]),
    deskTool({
      structure: pageStructure([home]),
    }),
  ],
})
Feb 21, 2023, 7:01 PM
Plugin code:
export const singletonPlugin = (types: string[]) => definePlugin({
    name: 'singletonPlugin',
    document: {
      newDocumentOptions: (prev, { creationContext }) => {
        if (creationContext.type === 'global') {
          return prev.filter(
            (templateItem) => !types.includes(templateItem.templateId)
          )
        }
        return prev
      },
    },
  })
Feb 21, 2023, 7:02 PM
as you can see they are pretty much the same. I think the issue is with how the plugin is either defined or added to the main config. Any ideas?
Feb 21, 2023, 7:07 PM
I tried console log from the plugin code but receive nothing
Feb 21, 2023, 7:09 PM
What’s not working with that singleton plugin? Is the item still showing up in the create new menu?
Feb 21, 2023, 9:11 PM
Hey
user M
yeah declared inside the config, it works, item doesnt show in the create new menu. when imported as a plugin the plugin doesnt seem to even instantiate (cant see a console log) and the item appears inside the create new menu
Feb 21, 2023, 9:14 PM
the
pageStructure
plugin which is declared and imported from the same ts file works as expected
Feb 21, 2023, 9:16 PM
ok I fixed it by removing
definePlugin

export const singletonPlugin = (types: string[]) => ({
  name: "singletonPlugin",
  document: {
    newDocumentOptions: (prev, { creationContext }) => {
      if (creationContext.type === "global") {
        console.log("prev in plugin", prev);
        return prev.filter(
          (templateItem) => !types.includes(templateItem.templateId)
        );
      }
      console.log("prev in plugin", prev);
      return prev;
    },
  },
});
Feb 21, 2023, 9:22 PM
ok nice one, I got the types back using PluginOptions:
export const singletonPlugin = (types: string[]): PluginOptions => ({
  name: "singletonPlugin",
  document: {
    newDocumentOptions: (prev, { creationContext }) => {
      if (creationContext.type === "global") {
        console.log("prev in plugin", prev);
        return prev.filter(
          (templateItem) => !types.includes(templateItem.templateId)
        );
      }
      console.log("prev in plugin", prev);
      return prev;
    },
  },
});
Feb 21, 2023, 9:27 PM
Maybe somethings up with my setup, it is an old codebase so maybe it's gubbed, I was following https://www.sanity.io/docs/developing-plugins though, when I get chance I'll try reproduce the problem and report an issue. Love the new v3 though!!
Feb 21, 2023, 9:30 PM
Interesting that
definePlugin
is interfering in that way! I checked my code and I have this for my singleton plugin:
export const singletonPlugin = types => ({
  name: 'singletonPlugin',
  document: {
    newDocumentOptions: prev =>
      prev.filter(template => !types.includes(template.templateId)),
    actions: (prev, { schemaType }) =>
      types.includes(schemaType)
        ? prev.filter(({ action }) => !['delete', 'duplicate'].includes(action))
        : prev,
  },
});
I’ll play around and see if I can get the same behavior you were experiencing.
Feb 21, 2023, 9:36 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?