问题
I need to add a custom batch action to my SonataAdmin entity that allows the user to select a number of items in the list view, then select the custom batch action (called 'edit dates') then... and here's where I'm stuck... display a form with two date fields that, when submitted, updates the selected list items with the inputted dates.
Is it even possible to have a multi-step batch action like this in SonataAdminBundle?
回答1:
You can add your date fields to the template:
{# in Acme/ProjectBundle/Resources/views/CRUD/list__batch.html.twig #}
{# See SonataAdminBundle:CRUD:list__batch.html.twig for the current default template #}
{% extends admin.getTemplate('base_list_field') %}
{% block field %}
<input type="checkbox" name="idx[]" value="{{ admin.id(object) }}" />
{# your date fields here #}
<input type="date" name="start" />
<input type="date" name="end" />
{% endblock %}
Source: 13.2. (Optional) Overriding the batch selection template
This will add your fields to each row.
If you only need the fields once e.g. in the footer (near the batch action select and export function) you can override the CRUD/base_list.html.twig template in your admin class:
public function getTemplate($name)
{
switch ($name) {
case 'list':
return 'MyBundle:MyAdmin:list.html.twig';
break;
default:
return parent::getTemplate($name);
break;
}
}
And then use the values inside your batchActionMultiStep() method.
来源:https://stackoverflow.com/questions/23996476/sonata-admin-bundle-add-a-multi-step-batch-action