Doctrine ORM EntityFilterType
The Doctrine ORM EntityFilterType represents a filter that operates on an entity values.
Options
choice_label
This is the property that should be used for displaying the entities as text in the active filter HTML element.
use App\Entity\Category;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type\EntityFilterType;
// ...
$builder->addFilter('category', EntityFilterType::class, [
'form_options' => [
'class' => Category::class,
'choice_label' => 'displayName', // choice label for form
],
'choice_label' => 'displayName', // separate choice label for data table filter
]);If left blank, the entity object will be cast to a string and so must have a __toString() method. You can also pass a callback function for more control:
use App\Entity\Category;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type\EntityFilterType;
use Kreyu\Bundle\DataTableBundle\Filter\FilterData;
// ...
$builder->addFilter('category', EntityFilterType::class, [
'form_options' => [
'class' => Category::class,
'choice_label' => 'displayName',
],
'choice_label' => function (FilterData $data): string {
return $data->getValue()->getDisplayName();
},
]);This option works like a choice_label option in ChoiceType form option.
When passing a string, the choice_label option is a property path. So you can use anything supported by the PropertyAccess component.
For example, if the translations property is actually an associative array of objects, each with a name property, then you could do this:
use App\Entity\Category;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type\EntityFilterType;
// ...
$builder->addFilter('category', EntityFilterType::class, [
'form_options' => [
'class' => Category::class,
'choice_label' => 'translations[en].name',
],
'choice_label' => 'translations[en].name',
]);Inherited options
label
- type:
null,false,stringorSymfony\Component\Translation\TranslatableInterface - default:
null
Sets the label that will be used when rendering the filter.
When value is null, a sentence cased filter name is used as a label, for example:
| Filter name | Guessed label |
|---|---|
| name | Name |
| firstName | First name |
label_translation_parameters
- type:
array - default:
[]
Sets the parameters used when translating the label option.
translation_domain
- type:
falseorstring - default:
'KreyuDataTable'
Sets the translation domain used when translating the translatable filter values.
Setting the option to false disables translation for the filter.
query_path
- type:
nullorstring - default:
nullthe query path is guessed from the filter name
Sets the path used in the proxy query to perform the filtering on.
form_type
- type:
string - default:
'Symfony\Bridge\Doctrine\Form\Type\EntityType'
This is the form type used to render the filter value field.
form_options
- type:
array - default:
['choice_value' => 'Identifier name of the entity class if 'class' form option is given, e.g. id']
This is the array that's passed to the form type specified in the form_type option.
operator_form_type
- type:
string - default:
'Kreyu\Bundle\DataTableBundle\Filter\Form\Type\OperatorType'
This is the form type used to render the filter operator field.
operator_form_options
- type:
array - default:
[]
This is the array that's passed to the form type specified in the operator_form_type option.
operator_selectable
- type:
bool - default:
false
Determines whether the operator can be selected by the user.
default_operator
- type:
Kreyu\Bundle\DataTableBundle\Filter\Operator - default:
Determines a default operator for the filter.
trim
- type:
bool - default:
false
Determines whether the TRIM() function should be applied on the expression. Uses the TrimExpressionTransformer transformer.
lower
- type:
bool - default:
false
Determines whether the LOWER() function should be applied on the expression for case-insensitive filtering. Uses the LowerExpressionTransformer transformer.
upper
- type:
bool - default:
false
Determines whether the UPPER() function should be applied on the expression for case-insensitive filtering. Uses the UpperExpressionTransformer transformer.
expression_transformers
- type:
ExpressionTransformerInterface[] - default:
[]
Defines expression transformers to apply on the expression.
use App\DataTable\Filter\ExpressionTransformer\UnaccentExpressionTransformer;
use Kreyu\Bundle\DataTableBundle\Type\AbstractDataTableType;
use Kreyu\Bundle\DataTableBundle\DataTableBuilderInterface;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\ExpressionTransformer\LowerExpressionTransformer;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\ExpressionTransformer\TrimExpressionTransformer;
use Kreyu\Bundle\DataTableBundle\Bridge\Doctrine\Orm\Filter\Type\TextFilterType;
class ProductDataTableType extends AbstractDataTableType
{
public function buildDataTable(DataTableBuilderInterface $builder, array $options): void
{
$builder
->addFilter('name', TextFilterType::class, [
'expression_transformers' => [
new LowerExpressionTransformer(),
new TrimExpressionTransformer(),
],
])
;
}
}For more information about expression transformers, read here.
