Sanity: [request] Follow array of references from preview method

Created on 22 Aug 2018  路  1Comment  路  Source: sanity-io/sanity

This may be a very specific use case, but it would be useful to retrieve an array of references for previews like so:

fields: [
  {
    title: 'Tags',
    name: 'tags',
    type: 'array',
    of: [ 
      { type: 'reference', to: { type: 'tag' } }
    ]
  }
],

preview: {
  select: {
    tags: 'tags[]->'
  }
}

Most helpful comment

We are planning to improve this, but in the mean time you can do something like this:

preview: {
  select: {
    tag0: 'tags.0.value',
    tag1: 'tags.1.value'
  },
  prepare(selection) {
    return {
      title: [selection.tag0, selection.tag1].join(', ')
    }
  }
}

Caveat: you must specify the full path of the value you want (I'm assuming the tag type has a value field here). E.g. selecting tags.1 will give you the reference value and that's probably not what you want here.

>All comments

We are planning to improve this, but in the mean time you can do something like this:

preview: {
  select: {
    tag0: 'tags.0.value',
    tag1: 'tags.1.value'
  },
  prepare(selection) {
    return {
      title: [selection.tag0, selection.tag1].join(', ')
    }
  }
}

Caveat: you must specify the full path of the value you want (I'm assuming the tag type has a value field here). E.g. selecting tags.1 will give you the reference value and that's probably not what you want here.

Was this page helpful?
0 / 5 - 0 ratings