Discussion on getting only currently used categories and customizing reference input components.
12 replies
Last updated: Oct 27, 2022
J
What is the best way to get only categories that are currently used? I have a category document that looks like the code snippet and then is used in my post document like so
I grab the categories to create a sitemap of them. However, I don't want to grab any that don't have articles under them (if that makes sense)
{ title: "Category", name: "category", description: "High level category of the article (FBS or FCS)", type: "string", options: { list: [ { title: "FCS", value: "FCS", }, { title: "FBS", value: "FBS", }, ], layout: "radio", // <-- defaults to 'dropdown' }, validation: (Rule) => Rule.required(), }, { title: "Subcategory", name: "subcategory", description: "What subcategory does this article belong to? Could be a conference, a team, etc.", type: "reference", to: [{ type: "category" }], options: { filter: "defined(parent)" }, },
Oct 26, 2022, 3:33 PM
R
I'd add a filter that counts the number of incoming references from articles. Something like this should get you categories with at least 1 article attached to them:
Haven't tested this, so you may need to tweak it a bit.
*[_type == 'category' && count(*[_type == 'article' && references(^._id)]) > 0]
Oct 26, 2022, 5:17 PM
J
Awesome! Looks like that worked! I did change
:gratitude-thank-you:
articleto
postfor my use case
export const getCategories = groq`*[_type == "category" && count(*[_type == 'post' && references(^._id)]) > 0]{ _id, title, _updatedAt, "slug": slug.current, description }`
:gratitude-thank-you:
Oct 26, 2022, 10:27 PM
R
Awesome!
Oct 26, 2022, 10:34 PM
J
I do have a question… Is there a way to make references in a bullet list format?
Oct 26, 2022, 10:35 PM
R
Inside of the Studio or when you're rendering it on the frontend?
Oct 26, 2022, 10:37 PM
J
What I mean by that... Is right now I am hardcoding the "parent" category by doing
and then the subcategory like I mention above... However, it would be nice to get the parent category like I do the subcategory... However, I like the radio layout for this and I haven't been able to figure that out
{ title: "Category", name: "category", description: "High level category of the article (FBS or FCS)", type: "string", options: { list: [ { title: "FCS", value: "FCS", }, { title: "FBS", value: "FBS", }, ], layout: "radio", // <-- defaults to 'dropdown' }, validation: (Rule) => Rule.required(), },
Oct 26, 2022, 10:37 PM
J
When I try to add
for example in the
layout: "radio"to
{ title: "Subcategory", name: "subcategory", description: "What subcategory does this article belong to? Could be a conference, a team, etc.", type: "reference", to: [{ type: "category" }], options: { filter: "defined(parent)" }, },
optionsobject, it doesn't actually do the radio... it is still a dropdown
Oct 26, 2022, 10:39 PM
R
Ah got it! You could, but it would need to be a custom input component. I made this component a while back for displaying references as a checklist, but you could adapt it to display a bullet list instead.
Oct 26, 2022, 10:41 PM
J
Awesome and then if I wanted to get the references that are parents, the filter under the options object would just be “parent” I’m assuming?
Oct 26, 2022, 10:45 PM
R
Exactly
Oct 26, 2022, 11:13 PM
J
Ok another question 😅 is there an easy way to update it? So right now I have
and want to change it to
However, I get what is in the screenshot
{ title: "Category", name: "category", description: "High level category of the article (FBS or FCS)", type: "string", options: { list: [ { title: "FCS", value: "FCS", }, { title: "FBS", value: "FBS", }, ], layout: "radio", // <-- defaults to 'dropdown' }, validation: (Rule) => Rule.required(), },
{ title: "Category", name: "category", description: "High level category of the article (FBS or FCS)", type: "reference", to: [{ type: "category" }], options: { filter: "parent" }, validation: (Rule) => Rule.required(), },
Oct 27, 2022, 3:17 AM
J
Also, is there a way to dynamically show the
Subcategoryoptions depending on which category is selected?
Oct 27, 2022, 3:17 AM
Sanity– build remarkable experiences at scale
The Sanity Composable Content Cloud is the headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.