Skip to content

Commit bcbef12

Browse files
committed
wip
1 parent b0155de commit bcbef12

File tree

4 files changed

+27
-110
lines changed

4 files changed

+27
-110
lines changed

src/app/Library/CrudPanel/SaveActions/AbstractSaveAction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,17 @@ public function getReferrerUrl(CrudPanel $crud, Request $request, $itemId = null
4040
{
4141
return null;
4242
}
43+
44+
public function toArray(): array
45+
{
46+
return [
47+
'name' => $this->getName(),
48+
'button_text' => $this->getButtonText(),
49+
'visible' => fn (CrudPanel $crud) => $this->isVisible($crud),
50+
'redirect' => fn (CrudPanel $crud, Request $request, $itemId = null) => $this->getRedirectUrl($crud, $request, $itemId),
51+
'referrer_url' => fn (CrudPanel $crud, Request $request, $itemId = null) => $this->getReferrerUrl($crud, $request, $itemId),
52+
'order' => $this->getOrder(),
53+
'_handler' => $this,
54+
];
55+
}
4356
}

src/app/Library/CrudPanel/SaveActions/ConfigArraySaveAction.php

Lines changed: 0 additions & 92 deletions
This file was deleted.

src/app/Library/CrudPanel/SaveActions/SaveActionInterface.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace Backpack\CRUD\app\Library\CrudPanel\SaveActions;
44

55
use Backpack\CRUD\app\Library\CrudPanel\CrudPanel;
6+
use Illuminate\Contracts\Support\Arrayable;
67
use Illuminate\Http\Request;
78

8-
interface SaveActionInterface
9+
interface SaveActionInterface extends Arrayable
910
{
1011
public function getName(): string;
1112

src/app/Library/CrudPanel/Traits/SaveActions.php

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ public function addSaveAction($saveAction)
103103
$saveAction['button_text'] = $saveAction['button_text'] ?? $saveAction['name'];
104104
$saveAction['order'] = isset($saveAction['order']) ? $this->orderSaveAction($saveAction['name'], $saveAction['order']) : $orderCounter;
105105

106+
if (isset($saveAction['_handler']) && $saveAction['_handler'] instanceof SaveActionInterface) {
107+
$saveAction['_handler']->setOrder((int) $saveAction['order']);
108+
}
109+
106110
$actions = $this->getOperationSetting('save_actions') ?? [];
107111

108112
if (! array_key_exists($saveAction['name'], $actions)) {
@@ -124,9 +128,15 @@ public function orderSaveAction(string $saveAction, int $wantedOrder)
124128
foreach ($actions as $key => $sv) {
125129
if (($sv['order'] ?? null) == $wantedOrder) {
126130
$actions[$key]['order'] = $replaceOrder;
131+
if (isset($actions[$key]['_handler']) && $actions[$key]['_handler'] instanceof SaveActionInterface) {
132+
$actions[$key]['_handler']->setOrder((int) $replaceOrder);
133+
}
127134
}
128135
if ($key == $saveAction) {
129136
$actions[$key]['order'] = $wantedOrder;
137+
if (isset($actions[$key]['_handler']) && $actions[$key]['_handler'] instanceof SaveActionInterface) {
138+
$actions[$key]['_handler']->setOrder((int) $wantedOrder);
139+
}
130140
}
131141
}
132142
$this->setOperationSetting('save_actions', $actions);
@@ -370,7 +380,7 @@ public function setupDefaultSaveActions()
370380
protected function normalizeSaveAction($saveAction): array
371381
{
372382
if ($saveAction instanceof SaveActionInterface) {
373-
return $this->convertSaveActionInstanceToArray($saveAction);
383+
return $saveAction->toArray();
374384
}
375385

376386
if (is_string($saveAction)) {
@@ -384,7 +394,7 @@ protected function normalizeSaveAction($saveAction): array
384394
throw new InvalidArgumentException(sprintf('Save action class [%s] must implement %s.', $saveAction, SaveActionInterface::class));
385395
}
386396

387-
return $this->convertSaveActionInstanceToArray($instance);
397+
return $instance->toArray();
388398
}
389399

390400
if (! is_array($saveAction)) {
@@ -397,19 +407,4 @@ protected function normalizeSaveAction($saveAction): array
397407

398408
return $saveAction;
399409
}
400-
401-
protected function convertSaveActionInstanceToArray(SaveActionInterface $action): array
402-
{
403-
$order = $action->getOrder();
404-
405-
return [
406-
'name' => $action->getName(),
407-
'button_text' => $action->getButtonText(),
408-
'visible' => fn ($crud) => $action->isVisible($crud),
409-
'redirect' => fn ($crud, $request, $itemId = null) => $action->getRedirectUrl($crud, $request, $itemId),
410-
'referrer_url' => fn ($crud, $request, $itemId = null) => $action->getReferrerUrl($crud, $request, $itemId),
411-
'order' => $order,
412-
'_handler' => $action,
413-
];
414-
}
415410
}

0 commit comments

Comments
 (0)