Hi đź‘‹
With my colleague @Leyllo we're encountering an issue with the relationship field.
We have two collections:
1).
articles
that contains a field
category
that can either be "Press" or "News"
2).
pages
that have the relationship field linked to
articles
.
So far we understand that the
filterOptions
parameter can be used to filter the current document (i.e. a page) through
data
or
siblingData
but we actually want to filter the choices for, for example, "any article that has a category of
news
".
Is this something possible? Should we go for another type of field to perform this?
In summary, we’re trying to mimic a taxonomy feature just like WordPress ACF but right now we cannot find a way to do the filtering that ACF allows.
Maybe is there a way to consume the local API (
payload.find()
) and display the response in a select, maybe through a custom UI component? Are there better options?
Thanks!
hi @Migu - this is absolutely possible, here is a simple way to setup with a relationship field:
{
name: 'articleType',
type: 'select',
options: [
{
value: 'news',
label: 'News',
},
{
value: 'press',
label: 'Press',
}
]
},
{
name: 'articles',
type: 'relationship',
relationTo: 'articles',
hasMany: true,
filterOptions: ({ data }) => {
return {
category: { equals: data.articleType },
};
},
}
The select field allows you to choose the category type. Then the relationship field will filter the results where the article category is equal to the category from your select field. If you don't need the option to toggle between them, you can set
equals: 'news'
or another static string.
Hope this helps!