🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Using child title to generate slug in Sanity.io schema

10 replies
Last updated: Jul 5, 2021
Hey all, is it possible to use the slug source from a child (type of array)?
Jul 2, 2021, 3:58 PM
How do you mean, sorry I'm new to this? I'm just trying to get the slug to generate from the child title not parent.
Jul 2, 2021, 4:08 PM
Oh, sorry. I think I misread. So you want
slug
for the object to generate from “Your First Visit?”
Jul 2, 2021, 4:12 PM
Yes, I;ve set the slug source to the child title in the js file but its pulling the source title from the parent.
Jul 2, 2021, 4:13 PM
Okay, I follow you. Thank you.
To accomplish this I think you’ll need to
return a function for source :

options: {
source: (doc) => {
return doc.child[0].title
}
}
Edit: The above isn’t quite right. It’ll base the slug on the first item in the array. You’ll want to access the second parameter of the source function. The underscore is convention for a parameter we don’t need.


options: {
  source: (_, { parent }) => {
    return parent.title
  }
}
Jul 2, 2021, 4:25 PM
Here’s a complete schema, in case the above isn’t clear:

export default {
  name: 'jeanmarc',
  title: 'Jean-Marc',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'title',
      type: 'string',
    },
    {
      name: 'child',
      title: 'Child',
      type: 'array',
      of: [
        {
          name: 'obj',
          title: 'Obj',
          type: 'object',
          fields: [
            {
              name: 'title',
              title: 'title',
              type: 'string',
            },
            {
              name: 'slug',
              title: 'Slug',
              type: 'slug',
              options: {
                source: (doc) => {
                  return doc.child[0].title
                }
              }
            }
          ]
        }
      ]
    }
  ]
}
Jul 2, 2021, 4:32 PM
Thanks for that! Apprecaite it 🙂 Will give it a shot.
Jul 2, 2021, 6:12 PM
Thanks for the reply!Would this work if im referencing the child document ? like the below

parent.js

name: "childCard",
title: "Child Services",
type: "array",
of: [{ type: "servicesChild" }],
Jul 2, 2021, 6:16 PM
Yes, this should work even when using references. I’ve edited my previous post as the first code wasn’t quite right.

export default {
  name: 'jeanmarc',
  title: 'Jean-Marc',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'title',
      type: 'string',
    },
    {
      name: 'childCard',
      title: 'Child Services',
      type: 'array',
      of: [{ type: 'servicesChild' }]
    }
  ]
}

export default {
  name: 'servicesChild',
  title: 'Child Services',
  type: 'document',
  fields: [
    {
      name: 'title',
      title: 'title',
      type: 'string',
    },
    {
      name: 'slug',
      title: 'Slug',
      type: 'slug',
      options: {
        source: (_, {parent}) => {
          return parent.title
        }
      }
    }
  ]
}
Jul 2, 2021, 6:43 PM
Thank you! I would love to learn more on this, is there any docuemntation on the methods we can pass?
Jul 3, 2021, 7:20 AM
I’m not aware of any docs other than what’s at https://www.sanity.io/docs/slug-type#source-9a89f442eaea , but if you pass in both arguments and then log the results, you can get some insight into what they enable.

options: {
  source: (doc, options) => {
    console.log('doc', doc);
    console.log('options', options);
  }
}
Jul 5, 2021, 3:02 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?