admin
admin copied to clipboard
Filter by relation's property
API Platform version(s) affected: 2.2.3 , 2.4.2
Description
I need to filter (SearchFilter) a resource with one of its association property. But in the filters in the admin page, I can see only the mobileNumber option.
How to reproduce
The documentation says it should be done with something like this:
* @ApiFilter(SearchFilter::class, properties={
* "mobileNumber": "partial",
* "label.title": "exact",
* })
here is the hydra:search section of the response :point_down::skin-tone-2:
hydra:search: {@type: "hydra:IriTemplate", hydra:template: "/users{?mobileNumber,label.title,label.title[]}",…}
@type: "hydra:IriTemplate"
hydra:mapping: [{@type: "IriTemplateMapping", variable: "mobileNumber", property: "mobileNumber", required: false},…]
0: {@type: "IriTemplateMapping", variable: "mobileNumber", property: "mobileNumber", required: false}
@type: "IriTemplateMapping"
property: "mobileNumber"
required: false
variable: "mobileNumber"
1: {@type: "IriTemplateMapping", variable: "label.title", property: "label.title", required: false}
@type: "IriTemplateMapping"
property: "label.title"
required: false
variable: "label.title"
2: {@type: "IriTemplateMapping", variable: "label.title[]", property: "label.title", required: false}
@type: "IriTemplateMapping"
property: "label.title"
required: false
variable: "label.title[]"
hydra:template: "/users{?mobileNumber,label.title,label.title[]}"
hydra:variableRepresentation: "BasicRepresentation"
I solved by setting filters manually. I have User with a ManyToOne relation to Company.
In the entity:
* @ApiFilter(SearchFilter::class, properties={
* "company": "exact"
* })
In the admin:
import React from 'react'
import { ListGuesser, FieldGuesser } from '@api-platform/admin'
import { AutocompleteInput, Filter, ReferenceField, ReferenceInput } from 'react-admin'
import IdField from '../../components/IdField'
const PostPagination = props => <Pagination rowsPerPageOptions={[10, 25, 50, 100, 250]} {...props} />;
const UserFilter = (props) => (
<Filter {...props}>
<ReferenceInput label="Company" source="company" reference="companies" allowEmpty alwaysOn>
<AutocompleteInput optionText="name" />
</ReferenceInput>
</Filter>
);
const List = ({ ...props }) => (
<ListGuesser {...props} filters={<UserFilter />}>
See the documentation of React-admin for more options: https://marmelab.com/react-admin/Inputs.html#referenceinput