Skip to content

Commit 59b45c4

Browse files
committed
Add support for registering and retrieving card actions in Board component
1 parent aae89a3 commit 59b45c4

File tree

5 files changed

+81
-9
lines changed

5 files changed

+81
-9
lines changed

resources/views/livewire/card.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
x-sortable-handle
1818
x-sortable-item="{{ $record['id'] }}"
1919
@if($hasCardAction && $cardAction)
20-
wire:click="mountAction('{{ $cardAction }}', { recordKey: '{{ $record['id'] }}' })"
20+
wire:click="mountAction('{{ $cardAction }}', [], @js(['recordKey' => $record['id']]))"
2121
style="cursor: pointer;"
2222
@endif
2323
>

src/Board.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class Board extends ViewComponent
3030

3131
protected array $filters = [];
3232

33+
/**
34+
* @var array<string, \Filament\Actions\Action>
35+
*/
36+
protected array $registeredCardActions = [];
37+
3338
final public function __construct()
3439
{
3540
// Board should be instantiated via make() method only
@@ -69,6 +74,37 @@ protected function resolveDefaultClosureDependencyForEvaluationByName(string $pa
6974
};
7075
}
7176

77+
/**
78+
* Override registerCardActionInstance to store Action objects.
79+
*/
80+
protected function registerCardActionInstance(\Filament\Actions\Action $action, \Illuminate\Database\Eloquent\Model | array $record): void
81+
{
82+
\Illuminate\Support\Facades\Log::info('Board::registerCardActionInstance called', [
83+
'action_name' => $action->getName(),
84+
'record_id' => is_array($record) ? ($record['id'] ?? 'unknown') : $record->getKey(),
85+
]);
86+
87+
$this->registeredCardActions[$action->getName()] = $action;
88+
}
89+
90+
/**
91+
* Get a registered card action by name.
92+
*/
93+
public function getRegisteredCardAction(string $name): ?\Filament\Actions\Action
94+
{
95+
return $this->registeredCardActions[$name] ?? null;
96+
}
97+
98+
/**
99+
* Get all registered card actions.
100+
*
101+
* @return array<string, \Filament\Actions\Action>
102+
*/
103+
public function getRegisteredCardActions(): array
104+
{
105+
return $this->registeredCardActions;
106+
}
107+
72108
protected function setUp(): void
73109
{
74110
parent::setUp();

src/BoardPage.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ abstract class BoardPage extends Page implements HasActions, HasBoard, HasForms
4141

4242
public function bootedInteractsWithActions(): void
4343
{
44-
// Recreate board fresh (Filament pattern)
45-
$this->board = $this->board(Board::make());
44+
// Get or create the board (don't overwrite existing configuration)
45+
$this->board = $this->getBoard();
4646

4747
// Set the query on the board if not already set
4848
if (! $this->board->getQuery()) {

src/Concerns/HasCardAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ public function getCardAction(Model | array $record): ?string
4949

5050
return $action::getDefaultName() ?? $action;
5151
}
52-
}
52+
}

src/Livewire/KanbanBoard.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ protected function cacheBoardActions(): void
190190
foreach ($boardPage->getBoard()->getRecordActions() as $action) {
191191
$this->cacheBoardAction($action);
192192
}
193+
194+
// Cache any registered card actions (from Action objects)
195+
foreach ($boardPage->getBoard()->getRegisteredCardActions() as $action) {
196+
$this->cacheBoardAction($action);
197+
}
193198
}
194199

195200
/**
@@ -412,7 +417,7 @@ public function getCardActionForRecord(array $recordData): ?string
412417
}
413418

414419
$board = $boardPage->getBoard();
415-
420+
416421
if (! method_exists($board, 'getCardAction')) {
417422
return null;
418423
}
@@ -424,7 +429,22 @@ public function getCardActionForRecord(array $recordData): ?string
424429
return null;
425430
}
426431

427-
return $board->getCardAction($recordModel);
432+
$actionName = $board->getCardAction($recordModel);
433+
434+
// If we have a registered card action, cache it for Filament's action system
435+
if ($actionName && ($registeredAction = $board->getRegisteredCardAction($actionName))) {
436+
\Illuminate\Support\Facades\Log::info('KanbanBoard::getCardActionForRecord caching action', [
437+
'action_name' => $actionName,
438+
'record_id' => $recordData['id'],
439+
]);
440+
441+
$actionClone = $registeredAction->getClone();
442+
$actionClone->livewire($this);
443+
$actionClone->record($recordModel);
444+
$this->cacheAction($actionClone);
445+
}
446+
447+
return $actionName;
428448
} catch (\Exception) {
429449
return null;
430450
}
@@ -551,17 +571,33 @@ public function getDefaultActionRecord(\Filament\Actions\Action $action): ?\Illu
551571
// Get the last (current) mounted action
552572
$currentMountedAction = end($mountedActions);
553573

554-
// Extract recordKey from the arguments (second parameter of mountAction)
555-
$recordKey = $currentMountedAction['arguments']['recordKey'] ?? null;
574+
// Extract recordKey from either context or arguments (both are supported)
575+
$recordKey = $currentMountedAction['context']['recordKey'] ??
576+
$currentMountedAction['arguments']['recordKey'] ?? null;
577+
578+
\Illuminate\Support\Facades\Log::info('KanbanBoard::getDefaultActionRecord called', [
579+
'action_name' => $action->getName(),
580+
'recordKey' => $recordKey,
581+
'context' => $currentMountedAction['context'] ?? [],
582+
]);
556583

557584
if (! $recordKey) {
558585
return null;
559586
}
560587

561588
// Resolve the record using our adapter
562-
return $this->getAdapter()->getModelById($recordKey);
589+
$record = $this->getAdapter()->getModelById($recordKey);
590+
591+
\Illuminate\Support\Facades\Log::info('KanbanBoard::getDefaultActionRecord resolved', [
592+
'recordKey' => $recordKey,
593+
'record_found' => $record !== null,
594+
'record_id' => $record ? $record->getKey() : null,
595+
]);
596+
597+
return $record;
563598
}
564599

600+
565601
/**
566602
* Render the component.
567603
*/

0 commit comments

Comments
 (0)