F
Filament7mo ago
Toni

Experiencing odd behaviour with custom Actions

Hi, I have a small application where the user can design email templates and publish them. Once published, the email templates can be used in other parts of the application. Therefore I wrote to actions: publish and unpublish Email. This is the code for the publish action:
class PublishMailAction extends Action
{
public static function getDefaultName(): ?string
{
return 'publishEmail';
}

protected function setUp(): void
{
parent::setUp();
$this->icon('fal-check-to-slot');
$this->label(__('mail.labels.publish'));
$this->action(static function ($record) {
if (MailServiceProvider::publishMailTemplate($record)) {
MailNotifications::mailTemplatePublishedSuccess()->send();
return true;
}
MailNotifications::mailTemplatePublishedFailed()->send();
return false;
});
}
}
class PublishMailAction extends Action
{
public static function getDefaultName(): ?string
{
return 'publishEmail';
}

protected function setUp(): void
{
parent::setUp();
$this->icon('fal-check-to-slot');
$this->label(__('mail.labels.publish'));
$this->action(static function ($record) {
if (MailServiceProvider::publishMailTemplate($record)) {
MailNotifications::mailTemplatePublishedSuccess()->send();
return true;
}
MailNotifications::mailTemplatePublishedFailed()->send();
return false;
});
}
}
The unpublish action is similar, except MailServiceProvider::publishMailTemplate. The table function from the resource:
->actions([
ActionGroup::make([
ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])->dropdown(false),
ActionGroup::make([
SendTestMailAction::make(),
PublishMailAction::make()
->visible(fn ($record) => !$record->is_published),
UnpublishMailAction::make()
->visible(fn ($record) => $record->is_published),
])->dropdown(false),
]),
])
->actions([
ActionGroup::make([
ActionGroup::make([
Tables\Actions\ViewAction::make(),
Tables\Actions\EditAction::make(),
])->dropdown(false),
ActionGroup::make([
SendTestMailAction::make(),
PublishMailAction::make()
->visible(fn ($record) => !$record->is_published),
UnpublishMailAction::make()
->visible(fn ($record) => $record->is_published),
])->dropdown(false),
]),
])
The Problem is, that it seems that as soon as I publish an email template, the state is preserved. I published the last email template, and when I click on another entry, the unpublish action is visible and all custom actions refer still to the first published item. What am I doing wrong? Thank you! Edit: I have forgotten the second screenshot:
No description
Solution:
Second screenshot:
No description
Jump to solution
17 Replies
Solution
Toni
Toni7mo ago
Second screenshot:
No description
Dennis Koch
Dennis Koch7mo ago
Those templates are using normal auto-incrementing IDs?
Toni
ToniOP7mo ago
Yes, the column is named id and is bigint unsigned auto_increment. Creation, deletion and update works like a charme. the interessting thing is, that this behaviour doesn't apply to the built in CRUD actions of Filament.
Dennis Koch
Dennis Koch7mo ago
Hm. Weird. Did you try outputting the id of the record during (appended to the label) to debug this?
Toni
ToniOP7mo ago
Yes, right now after your suggestion. Every record has the same ID of the first published/unpublished record
Dennis Koch
Dennis Koch7mo ago
But only for your actions? Not for the prebuilt ones?
Toni
ToniOP7mo ago
Right
Dennis Koch
Dennis Koch7mo ago
Same behaviour if you move them to the same action group?
Toni
ToniOP7mo ago
No description
No description
No description
Dennis Koch
Dennis Koch7mo ago
Those actions are table actions, right? Not generic actions
Toni
ToniOP7mo ago
Hahaha, no you are right. As soon as i put them in the same action group it works as expected Yes
Dennis Koch
Dennis Koch7mo ago
Hm. I think I haven seen something like ->key('group-one') somewhere. Does that method exists and does giving them unique keys work?
Toni
ToniOP7mo ago
No, I don't use ->key('group-one') For me the issue is solved, I don't need to you nested action groups. Should I open a ticket in GitHub? Thank you again for your assistance (second time this weekend 😉
Dennis Koch
Dennis Koch7mo ago
If you have the time to provide a reproduction repo, I'd appreciate it. Gerne 😉
Toni
ToniOP7mo ago
Is this just a common repo with public access or are ther some specialities to consider?
Dennis Koch
Dennis Koch7mo ago
Just a fresh install of Filament with a simple example that shows the issue
Toni
ToniOP7mo ago
okidoki, will do it Done.
Want results from more Discord servers?
Add your server