Skip to content

Commit 8940341

Browse files
author
Kinhelm
committed
feature(BatchSelection): add choice to display or not the modal confirmation
1 parent 2bc71ea commit 8940341

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

assets/js/app.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -338,16 +338,24 @@ class App {
338338
.replace('%action_name%', actionName)
339339
.replace('%num_items%', selectedItems.length.toString());
340340

341+
if (actionElement.getAttribute('data-display-batch-modal-confirmation') === 'false') {
342+
batchFormSubmit(actionElement, selectedItems);
343+
}
344+
341345
document.querySelector('#modal-batch-action-button').addEventListener('click', () => {
342346
// prevent double submission of the batch action form
343347
actionElement.setAttribute('disabled', 'disabled');
348+
batchFormSubmit(actionElement, selectedItems);
349+
});
344350

351+
function batchFormSubmit(actionElement, selectedItems) {
345352
const batchFormFields = {
346353
'batchActionName': actionElement.getAttribute('data-action-name'),
347354
'entityFqcn': actionElement.getAttribute('data-entity-fqcn'),
348355
'batchActionUrl': actionElement.getAttribute('data-action-url'),
349356
'batchActionCsrfToken': actionElement.getAttribute('data-action-csrf-token'),
350357
};
358+
351359
selectedItems.forEach((item, i) => {
352360
batchFormFields[`batchActionEntityIds[${i}]`] = item.value;
353361
});
@@ -365,7 +373,7 @@ class App {
365373

366374
document.body.appendChild(batchForm);
367375
batchForm.submit();
368-
});
376+
}
369377
});
370378
});
371379
}

doc/actions.rst

+14
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,20 @@ they can link to a CRUD controller method, to a Symfony route or to some URL.
441441
If there's at least one batch action, the backend interface is updated to add some
442442
"checkboxes" that allow selecting more than one row of the index listing.
443443

444+
By default, batch processes open a confirmation modal before proceeding with the action.
445+
This is useful for batch deletions.
446+
However, if you do not want the confirmation modal, use the ``displayBatchConfirmationModal()`` method::
447+
448+
// ...
449+
450+
return $actions
451+
->addBatchAction(Action::new('approve', 'Approve Users')
452+
->linkToCrudAction('approveUsers')
453+
->addCssClass('btn btn-primary')
454+
->setIcon('fa fa-user-check')
455+
->displayBatchConfirmationModal(false) // <- here
456+
;
457+
444458
When the user clicks on the batch action link/button, a form is submitted using
445459
the ``POST`` method to the action or route configured in the action. The easiest
446460
way to get the submitted data is to type-hint some argument of your batch action

src/Config/Action.php

+7
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ public function createAsBatchAction(): self
8888
return $this;
8989
}
9090

91+
public function displayBatchConfirmationModal(bool $value = true): self
92+
{
93+
$this->dto->setBatchConfirmationModal($value);
94+
95+
return $this;
96+
}
97+
9198
/**
9299
* @param TranslatableInterface|string|false|null $label Use FALSE to hide the label; use NULL to autogenerate it
93100
*/

src/Dto/ActionDto.php

+11
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ final class ActionDto
2727
private $url;
2828
private array $translationParameters = [];
2929
private $displayCallable;
30+
private ?bool $batchConfirmationModal = true;
3031

3132
public function getType(): string
3233
{
@@ -261,6 +262,16 @@ public function setDisplayCallable(callable $displayCallable): void
261262
$this->displayCallable = $displayCallable;
262263
}
263264

265+
public function getBatchConfirmationModal(): ?bool
266+
{
267+
return $this->batchConfirmationModal;
268+
}
269+
270+
public function setBatchConfirmationModal(?bool $batchConfirmationModal): void
271+
{
272+
$this->batchConfirmationModal = $batchConfirmationModal;
273+
}
274+
264275
/**
265276
* @internal
266277
*/

src/Factory/ActionFactory.php

+7-1
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,19 @@ private function processAction(string $pageName, ActionDto $actionDto, ?EntityDt
159159

160160
if ($actionDto->isBatchAction()) {
161161
$actionDto->addHtmlAttributes([
162-
'data-bs-toggle' => 'modal',
163162
'data-bs-target' => '#modal-batch-action',
164163
'data-action-csrf-token' => $this->csrfTokenManager?->getToken('ea-batch-action-'.$actionDto->getName()),
165164
'data-action-batch' => 'true',
166165
'data-entity-fqcn' => $adminContext->getCrud()->getEntityFqcn(),
166+
'data-display-batch-modal-confirmation' => $actionDto->getBatchConfirmationModal() ? 'true' : 'false',
167167
'data-action-url' => $actionDto->getLinkUrl(),
168168
]);
169+
170+
if ($actionDto->getBatchConfirmationModal()) {
171+
$actionDto->addHtmlAttributes([
172+
'data-bs-toggle' => 'modal',
173+
]);
174+
}
169175
}
170176

171177
return $actionDto;

0 commit comments

Comments
 (0)