Replies: 1 comment
-
Hello @dbu, maybe EntityFilterType would be useful for you? In my demo app I have some simple examples (link, link). ->addFilter('owner', EntityFilterType::class, [
'choice_label' => function (User $user): string {
// shorter label without department name
return $user->getFirstName().' '.$user->getLastName();
},
'form_options' => [
'choice_label' => function (User $user): string {
// display department name in case there are multiple users with the same name
return $user->getFirstName().' '.$user->getLastName().' ('.$user->getDepartment()->getName().')';
},
'class' => User::class,
'query_builder' => function (EntityRepository $er): QueryBuilder {
return $er->createQueryBuilder('u')
->innerJoin('u.department', 'd')
->addSelect('d')
->addOrderBy('u.lastName', 'ASC')
->addOrderBy('u.firstName', 'ASC')
;
},
],
'label' => 'document.owner',
]) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
i managed to build a choice filter. i have ORM entities with foreign keys, and therefor want to filter my entities on ids from that foreign table, but of course i want to show the human readable names of the related entities.
my first attempt was to use a NumericFilterType:
this worked almost fine, except that the active filter is shown with the key instead of the value. i did not find a way to change how the value is rendered. i tried to hook into the FilterEvents::POST_HANDLE event, but my event never got called (i tried to debug that a bit, i got the impression that the FilterConfigInterface::getEventDispatcher does not return the normal symfony event dispatcher, as my event listener is registered according to symfony debug, but the place that checks if it should call dispatch does not see it. i then gave up on that route.)
so instead i created a ChoiceFilterType that allows to set a custom formatter for the value
and i can use this filter in a table builder:
i experimented with a formatter callback that the options support and that buildView would call if set. but that approach seemed too complicated to me for this use case, we should have the same text for the active filter as is used in the choice dropdown for the filter.
as this seems a pretty common use case, i could contribute this as a built-in filter if you want. maybe we could make it more elegant even, to make the choices a top-level configuration instead of "leaking" the form type configuration to the filter type usage. or i can contribute it as an example to the documentation.
or does the bundle already provide this functionality and i did not find it?
Beta Was this translation helpful? Give feedback.
All reactions