feat: filtering for relation widget (#2405) #7161
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR adds a
filters
field for the Relation widget which allows adding filters by which the available options are filtered on. This was requested in issue #2405 (and also by myself :)).How it works is that you can add "filters" which are a pair of
field
and the allowedvalues
, where the widget will only show options (collection items) that satisfy all the "filters". An collection item satisfies a filter if the value offield
is one of the values invalues
.Example
In this example the relation widget in the restaurants collection will only show and allow options that are posts which are not a draft (i.e.
draft
field isfalse
). Say, we have 20 posts in the CMS of which 5 havedraft
set to true, then we will only see the other 15 posts as option in the relation widget.Multiple filters can be added:
In this case only the posts with
draft
set tofalse
and a title that is either 'posts about cats' or 'post about dogs' will be options seen in the relation widget.Test plan
Added the following unit tests:
After adding the following filter to the dev-test
config.yml
:filters: [ {field: "draft", values: [false]} ]
These are the results of the filter:
draft
with valuefalse
gives the following possible options: (note that without the filter you would see posts 1 to 20 by default)Checklist
Please add a
x
inside each checkbox:A picture of a cute animal (not mandatory but encouraged)
my 15 week old pup :)