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

How to hide links in Sanity Studio when editor has not given a value

14 replies
Last updated: Sep 9, 2021
Hi, I am trying to hide a link when the editor has not given a value, can anyone help me?
Sep 8, 2021, 9:59 AM
In Sanity Studio? Maybe Conditional Fields is something you’re looking for.
Sep 8, 2021, 11:28 AM
I would like some help setting it up
Sep 9, 2021, 7:38 AM
Sure thing. Do you have any existing schema code? If so, feel free to post it in this thread inside a code block (created with `````). You can post the whole thing or just the relevant code for the item you want to hide.
Sep 9, 2021, 2:01 PM
export default {
  name: "project",
  title: "Project",
  type: "document",
  fields: [
    {
      name: "title",
      type: "string",
    },
    {
      name: "subtitle",
      type: "string",
    },
    {
      name: "mainImage",
      title: "Main image",
      type: "image",
      options: {
        hotspot: true,
      },
    },
    {
      name: "date",
      type: "datetime",
    },
    {
      name: "place",
      type: "string",
    },
    {
      name: "description",
      type: "text",
    },
    {
      name: "projectType",
      title: "Project type",
      type: "string",
      options: {
        list: [
          { value: "personal", title: "Personal" },
          { value: "client", title: "Client" },
          { value: "school", title: "School" },
        ],
      },
    },
    {
      name: "link",
      title: "Link",
      type: "url",
    },
    {
      name: "ghlink",
      title: "GitHub Link",
      type: "url",
    },
  ],
};
Sep 9, 2021, 5:22 PM
I want to hide the links if the editor has not given a value
Sep 9, 2021, 5:23 PM
I want to hide the links if the editor has not given a value
Sep 9, 2021, 5:23 PM
A value to which field?
Sep 9, 2021, 5:24 PM
{
      name: "link",
      title: "Link",
      type: "url",
    },
    {
      name: "ghlink",
      title: "GitHub Link",
      type: "url",
    },
  ],
};
Sep 9, 2021, 5:25 PM
Sorry, which field needs a value to unhide the link fields?
Sep 9, 2021, 5:25 PM
The
link
field and
ghlink
field
Sep 9, 2021, 5:30 PM
In that case, if they are empty there isn’t an easy way to get them to show using the studio (since they’re not visible to type into). You’d need to populate those fields using the API or CLI.
Sep 9, 2021, 5:31 PM
But something like this should work:

{
  name: "link",
  title: "Link",
  type: "url",
  hidden: ({ value }) => !value,
},
{
  name: "ghlink",
  title: "GitHub Link",
  type: "url",
  hidden: ({ value }) => !value,
},

Sep 9, 2021, 5:32 PM
Thank you
Sep 9, 2021, 5:42 PM
You’re welcome! 🙂
Sep 9, 2021, 5:42 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?