Keystone: Limit (filter) Relationship to a sub-set of List items

Created on 25 Oct 2018  路  4Comments  路  Source: keystonejs/keystone

In K4 it was possible to define a filter on a Relationship field type. This allowed restricting relationships created through the admin ui to a subset of a List.

Related to: #376, #352

Example use cases

1) A Post has a relationship field for the assigned reviewer. Only User's with isAdmin === true can be assigned.

In K4 this was defined on the List as follows:

reviewer: {
    type: Relationship,
    ref: 'User',
    filters: { isAdmin: true },
},

2) A Site can have featuredTags. Those Tags must be related to the current site. ie:

featuredTags: {
    type: Relationship,
    ref: 'Tag',
    many: true,
    filters: { site: ':_id' },
},
Relationship admin-ui needs-review

Most helpful comment

Yes that's one side of it. The other side is that in the Admin UI you are not presented with invalid options when creating the relationship.

All 4 comments

This could be possible when we expose the hooks:

keystone.createList('Post', {
  fields: {
    reviewer: {
      type: Relationship,
      ref: 'User',
      hooks: {
        preUpdate: (data, item, context) => {
          if (!data.isAdmin) throw new Error('Not allowed');
        }
      }
    }
  }
});

Yes that's one side of it. The other side is that in the Admin UI you are not presented with invalid options when creating the relationship.

It looks like you haven't had a response in over 3 months. Sorry about that! We've flagged this issue for special attention. It wil be manually reviewed by maintainers, not automatically closed. If you have any additional information please leave us a comment. It really helps! Thank you for you contributions. :)

The view section should now be done as a custom view. This use-case becomes rather simple if we land: https://github.com/keystonejs/keystone/pull/2885

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gautamsi picture gautamsi  路  14Comments

ra-external picture ra-external  路  10Comments

thekevinbrown picture thekevinbrown  路  31Comments

bothwellw picture bothwellw  路  18Comments

bholloway picture bholloway  路  18Comments