Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

FilterOptions for Array contains Array

default discord avatar
davedamage2 years ago
4

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

  • discord user avatar
    jarrod_not_jared
    2 years 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
    davedamage2 years 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,
            };
          },
        },
  • default discord avatar
    jessrynkar2 years ago

    hi

    @381079209197699083

    - 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
    davedamage2 years ago

    Thanks that solved it

Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.