A
A
Alexey Sobolenko2019-07-12 20:49:00
symfony
Alexey Sobolenko, 2019-07-12 20:49:00

How to properly set up a filter by roles in SonataAdminBundle?

I sifted through the SonataAdminBundle documentation, but did not find anything suitable.
If you specify in configureDatagridFilters like this: the filter always works well, except for one case - when choosing advanced filters and is equal to , everything is because of the tricky form of writing roles to the database: a:1:{i:0;s:9;" ROLE_USER";} I decided to use a callback to search
$datagridMapper->add('roles');

5d28c54a9765b405660929.png

$datagridMapper
            ->add(
                'roles',
                CallbackFilter::class,
                [
                    'operator_type' => TextType::class,
                    'callback' => [$this, 'filterByRoles'],
                ]
            );

and implemented the function, but
5d28c7e4ee940175340578.png
the select was missing here. I think I've specified something wrong in configureDatagridFilters , but I can't figure out what.
Actually, the filter works as it should, just replace the text field (in the second picture) with a select, as in the first picture.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Sobolenko, 2019-07-17
@alexsobolenko

$datagridMapper->add(
                'roles',
                CallbackFilter::class,
                [
                    'operator_type' => ChoiceType::class,
                    'callback' => [$this, 'filterByRoles'],
                    'operator_options' => [
                        'choices'=> [
                            "contains" => 1,
                            "not contains" => 2,
                            "is equals to" => 3,
                        ],
                    ],
                ],
                ChoiceType::class,
                [
                    'choices' => [
                        'USER' => 'ROLE_USER',
                        'ADMIN' => 'ROLE_ADMIN',
                        'SUPPORTER' => 'ROLE_SUPPORTER',
                        'COPYWRITER' => 'ROLE_COPYWRITER',
                        'SUPER_ADMIN' => 'ROLE_SUPER_ADMIN',
                    ],
                    'multiple' => true,
                ]
            );

K
kafkiansky, 2019-07-12
@mad_maximus

If anything, the roles do not have to be stored as the symphony says in the documentation. It can also be simple strings. What matters is how you return the roles for the symphony. In order not to suffer much, you can do this:

->add('role', ChoiceType::class, [
                'choices' => [
                    'Пользователь' => Role::ROLE_USER,
                    'Автор' => Role::ROLE_AUTHOR,
                    'Редактор' => Role::ROLE_EDITOR,
                    'Главред' => Role::ROLE_CHIEF_EDITOR,
                    'Администратор' => Role::ROLE_ADMIN,
                ],
                'required' => false,
                'placeholder' => 'Роль',
            ])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question