Symfony 4 / Sonata add a filter linked to a OneToMany entity

Filters are managed in the configureDatagridFilters method

protected function configureDatagridFilters(DatagridMapper $datagridMapper)
	{
$filers=$datagridMapper->getAdmin()->getFilterParameters();
}



One can add a simple filter on a title or any parameter of our entity.
The show_filter parameter allows to display it when the page loads. Combined with the value of $filters, one can specify to keep the element displayed after the filter submission.

For example, one can set the filter to be permanently displayed:

$datagridMapper
            ->add('title', null, ['show_filter' => true , 'label' => 'Titre'])

Or to display it only if it has been used

->add('enabled',null,['show_filter' =>!empty($filers['enabled']['value'])?true:null, 'label' => 'Actvé'])

For a oneToMany relationship, if your entity is correctly configured, you can simply configure as follows:

$datagridMapper->add('field', null, ['show_filter' => true , 'label' => 'nom du champs']);

Basically, there is nothing to do. But we might need to filter the values. In this case, we will use doctrine to modify the selection query for the values:

->add('slug', 'doctrine_orm_callback', ['show_filter' =>!empty($filers['slug']['value'])?true:null,  'label' => 'SEO Slug',
                'callback'  => function($queryBuilder, $alias, $field, $value)
                        {
                            $queryBuilder->andWhere($queryBuilder->expr()->like('o.slug', "'%".$value["value"]."%'"));
		                }
            ])

We might also want to make a list dynamically built by ourselves:

 ->add('liste', 'doctrine_orm_choice', array(
                'label' => 'ma liste'),
                'choice',
                array(
                    'choices' => array(
                        'label value1' => 'value1',
                        'label value2' => 'value2',
                    ),
                    'expanded' => true,
                    'multiple' => true))

Please note that the placeholders `{{code_placeholder_X}}` were left untranslated, as they seem to represent placeholders for code snippets that should be replaced by actual code, which typically should not be translated. If you require the surrounding instructions or comments within the code snippets to be translated, please provide the actual code or comments.