Override CRUDController to add custom form elements to list template for batch action usage in SonataAdminBundle

0 Flares Twitter 0 Facebook 0 LinkedIn 0 0 Flares ×

In my use case I needed a batch action with an additional date field to be set on my Entity. I came up with the following solution.

I will not add the Entities since this post should focus on the configuration of the list template and batch action.

 

First of all define the custom batch action and the path to your custom list template in your Admin class:

 

Next enable and configure a custom CRUDController:

 

Then create your custom CRUDController:

 

Copy the original listAction from CRUDController.php and add the custom form date element:

We can use the sonata_type_date_picker which comes built-in with the SonataAdminBundle.

Next add the date picker to your template:

This should render some HTML and Javascript for the element looking like this:

 

In our Admin class we set ask_confirmation to true. This way we can make use of the relevancy check function to validate our new form element.

Regarding the naming conventions add a batchActionTestIsRelevant method to your CRUDController:

 

Finally we add the batchActionTest method:

And we’re done!

 

Backlinks:

Web Deviloper

  • Pingback: Override CRUDController to add custom form elem...()

  • Very useful info, except 1 error : in admin.yml, the custom CRUD controller param should be (bundle name)AcmeDemoBundle:ElectricalEquipmentAdmin instead of (namespace) Acme/DemoBundle:ElectricalEquipementAdmin

    • Thanks! Fixed. In future post I will use the default AppBundle namespace.

  • Cassiano Tartari

    And how about a batch update? I’m trying to do it with less override as possible. I can’t find a good way to redirect to a form to batch edit objects. The form is already sent to twig in batchAction just need to add a custom template for the custom batch action. And getBatchActions is deprecated since version 3.2., you should use configureBatchActions instead.