FilterOptions for Array contains Array

default discord avatar
Dave Damage
7 months ago
5

Hi,



I have the following Issue:



I have a category collection and a module collection, both of these have a technologies field (relationship field with hasMany option for a different collection). In my module field, I want to have a category field which automatically filters the available categories by the technologies of the module.



My main idea is to write a custom function that maps over the technologies of the module and checks if the technology is in the array of the categories' technology field and combine it with the OR clause.



I am honestly not sure if that is a viable solution and maybe there is something simpler, so if anybody has an idea it would be cool to hear it. Thanks in advance

  • default discord avatar
    Jarrod
    7 months ago

    Have you seen the filterOptions property on relationship fields? It might be just what you are looking for

    https://payloadcms.com/docs/fields/relationship#filtering-relationship-options
  • default discord avatar
    Dave Damage
    7 months ago

    Yeah, that's what I am using. Sorry for not mentioning that



        {
          name: 'category',
          label: 'Themenblock',
          type: 'relationship',
          required: true,
          relationTo: 'categories',
          filterOptions: ({ data }) => {
            const query = data?.technologies?.map((id) => ({
              technologies: {
                in: id,
              },
            }));
            return {
              or: query,
            };
          },
        },
  • discord user avatar
    jesschow
    Payload Team
    6 months ago

    hi @Dave Damage - I think you're on the right path here. I would expect the filterOptions query to look something like this:



          filterOptions: ({ data }) => {
            return {
              technologies: { in: data.technologies },
            };
          },
  • default discord avatar
    Dave Damage
    6 months ago

    Thanks that solved it

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.