Bulk actions

Tick rows (or all matches of the filter) and run an action on them: with options, with a dialog asking for values, or with a confirmation.

The code

app/src/Controllers/ProjectController.php – defineBulkActions() whole file
protected function defineBulkActions(): array
{
return [
BulkAction::create('status', 'Set status to', function (DataList $projects, ?string $status) {
foreach ($projects as $project) {
$project->Status = $status;
$project->write();
}
return $projects->count() . ' projects changed.';
})->options(Project::STATUSES),

BulkAction::create('assign', 'Assign manager …', function (DataList $projects, array $data) {
foreach ($projects as $project) {
$project->ManagerID = (int) $data['ManagerID'];
$project->Priority = (string) $data['Priority'];
$project->write();
}
return $projects->count() . ' projects assigned.';
})
->fields(fn () => FieldList::create(
DropdownField::create('ManagerID', 'Manager', $this->managerOptions())->setHasEmptyDefault(true),
DropdownField::create('Priority', 'Priority', Project::PRIORITIES)->setValue('normal')
), ['ManagerID'])
->submitLabel('Assign'),

BulkAction::create('delete', 'Delete', function (DataList $projects) {
$count = $projects->count();
foreach ($projects as $project) {
$project->delete();
}
return $count . ' projects deleted.';
})->confirm('Delete {count} projects?'),
];
}