Sanity: Slug source not resolving references

Created on 19 Feb 2020  路  3Comments  路  Source: sanity-io/sanity

Is your feature request related to a problem? Please describe.
N/A

Describe the solution you'd like
References should be resolved in the slugify source, e.g.

source: doc => `flights-from-${doc.title}-${doc.origin.name}`

Describe alternatives you've considered
Implementing a slugify function that resolves the references.

Additional context
N/A

Bug

Most helpful comment

Thanks again for reporting! This was fixed in v1.148.6 which was just released :)

All 3 comments

Thanks for reporting!

This looks like a bug. In order to get this working, you'd need to fetch the referenced document to access the fields in it. I tried this, but it didn't actually work. I have added it to our internal bug tracker. You can follow this issue to get notified when it's fixed.

This is the implementation that should've worked.

// schemas/post.js
import client from 'part:@sanity/base/client';

export default {
  name: 'post',
  type: 'document',
  title: 'Post',
  fields: [
    {
      name: 'title',
      type: 'string',
      title: 'Title',
      description: 'A short and sweet title',
    },
    {
      name: 'slug',
      type: 'slug',
      title: 'Slug',
      options: {
        source: async doc => {
          if (!doc.author) {
            return doc.title;
          }
          const author = await client.getDocument(doc.author._ref);
          return `${doc.title}-from-${author.name}`;
        },
      },
    },
    {
      name: 'author',
      type: 'reference',
      title: 'Author',
      to: [
        {
          type: 'person',
        },
      ],
    },
  ],
};

Thanks again for reporting! This was fixed in v1.148.6 which was just released :)

Thanks for the solution. I definitely think this should go into the docs on the website as an example!

Was this page helpful?
0 / 5 - 0 ratings