Question about SyliusGrid custom bulk action


#1

Hi,

I am trying to create a custom bulk action for a custom grid in the admin area. I followed the description in https://docs.sylius.com/en/1.3/components_and_bundles/bundles/SyliusGridBundle/custom_bulk_action.html

All works fine except the Controller does not receive the selected ids.

At the end of the docs mentioned above is written: "Let’s assume that you already have a route for exporting by injecting ids, " and I have no idea how this is supposed to be. I tried several options but without success.

Has someone a complete example of a custom bulk action or can someone indicate an example in the Sylius source code that is not the default bulk action DELETE?

Thank you very much in advance.


#2

The route that is mentioned in the documentation is the one used for button action, i.e. app_admin_product_export in that case.
Can you share you grid configuration with the bulk action?


#3

Hi vvasiloi,

but how is the route app_admin_product_export configured to accept the ids selected in the grid?

here is my configuration:
app_admin_erp_product:
driver:
name: doctrine/orm
options:
class: App\Entity\ErpProduct
actions:
bulk:
importBulk:
type: importBulk
label: app.ui.import_products
options:
link:
route: admin_import_erp_products
item:
import:
type: import
label: app.ui.import
options:
link:
route: admin_import_erp_product
parameters:
id: resource.id
fields:
field1:
type: string
label: app.ui.field1
field2:
type: string
label: app.ui.field2
filters:
field1:
type: string
label: app.ui.field1
field2:
type: string
label: app.ui.field2


#4

You will have the ids as a request parameter.
As example, look at sylius_admin_product_variant_bulk_delete in vendor/sylius/sylius/src/Sylius/Bundle/AdminBundle/Resources/config/routing/product_variant.yml.
Also, look at the bulk delete action: Sylius\Bundle\ResourceBundle\Controller\ResourceController::bulkDeleteAction


#5

This implies that my controller has to be a ResourceController. I tried this solution but I wasn’t able to make this run. It would be nice to have in the docs an example that uses a completely new Controller.

A team colleague found a very easy solution modifying the twig template for the bulk button putting the button inside an form as submit button. So the Controller receives the ids via POST method.

What do you think of this solution?


#6

You can use it with any controller. I pointed you to the way it’s done for bulk delete with the ResourseController.
I think you could even copy/paste the bulkDeleteAction method and it’s routing configuration and adjust it to your needs.
It already has the code to handle the bulk actions requests.
The dirty work will be done by sylius.resource_controller.request_configuration_factory and sylius.resource_controller.resources_collection_provider sevices, a repository method to find the resources by ids and finally the routing configuration that includes it:

custom_bulk_action
    path: /bulk-action
    methods: [DELETE]
    defaults:
        _controller: app.controller.your_custom_controller
        _sylius:
            repository:
                method: findById
                arguments: [$ids]

Let me know how it goes.


#7

Thanks vvasiloi. I will try this solution. Looks to be a nicer solution.