How to replace default actions in the Create and Edit pages?

I'm trying to build something similar to WordPress with Filament whereby a record can be published or saved as a draft. So basically I need to override the default Create functionality. I would like to remove the default "Create" and "Create & create another" buttons Add add these three custom actions instead "Save Draft", "Publish", "Schedule" and on the EditPage if $record->isPublished() an "Unpublish" action will be rendered instead of "Publish"
6 Replies
toeknee
toeknee10mo ago
->actions([My Actions here])
@nasilemak
@nasilemakOP10mo ago
Isn't that for the list page/table? I'm referring to the Create and Edit pages This is what I have so far. It's kind of messy.
class EditPost extends EditRecord
{
protected static string $resource = PostResource::class;

protected function getHeaderActions(): array
{
return [
Actions\Action::make('publish')
->label('Publish')
->action(function ($record) {
$record->publish();
$record->save();

Notification::make()
->title('Article has been published')
->icon('heroicon-o-document-text')
->iconColor('success')
->send();

return redirect(PostResource::getUrl('index'));
})
->visible(fn ($record): bool => $record && !$record->isPublished()),
Actions\Action::make('unpublish')
->label('Unpublish')
->action(function ($record) {
$record->unpublish();
$record->save();

Notification::make()
->title('Article is now unpublished and hidden from readers')
->icon('heroicon-o-document-text')
->iconColor('success')
->send();

return redirect(PostResource::getUrl('index'));
})
->visible(fn ($record): bool => $record && $record->isPublished()),
Actions\DeleteAction::make(),
];
}
}
class EditPost extends EditRecord
{
protected static string $resource = PostResource::class;

protected function getHeaderActions(): array
{
return [
Actions\Action::make('publish')
->label('Publish')
->action(function ($record) {
$record->publish();
$record->save();

Notification::make()
->title('Article has been published')
->icon('heroicon-o-document-text')
->iconColor('success')
->send();

return redirect(PostResource::getUrl('index'));
})
->visible(fn ($record): bool => $record && !$record->isPublished()),
Actions\Action::make('unpublish')
->label('Unpublish')
->action(function ($record) {
$record->unpublish();
$record->save();

Notification::make()
->title('Article is now unpublished and hidden from readers')
->icon('heroicon-o-document-text')
->iconColor('success')
->send();

return redirect(PostResource::getUrl('index'));
})
->visible(fn ($record): bool => $record && $record->isPublished()),
Actions\DeleteAction::make(),
];
}
}
Also I want the 'Publish' action to validate and save the form data the same way 'Create' and 'Save Changes' would
toeknee
toeknee10mo ago
protected function getActions(): array { } I beleive too for the footer actions You would then use $this->form->getState() usually, or $this->form->validate() in native livewire so should also work
@nasilemak
@nasilemakOP10mo ago
Yes that did the trick. Here's the updated code:
Actions\Action::make('publish')
->action(function ($record) {

$this->form->validate();
$record->update($this->form->getState());
$record->publish();
$record->save();

return redirect(PostResource::getUrl('index'));
})
Actions\Action::make('publish')
->action(function ($record) {

$this->form->validate();
$record->update($this->form->getState());
$record->publish();
$record->save();

return redirect(PostResource::getUrl('index'));
})
@nasilemak
@nasilemakOP10mo ago
Not sure if I understood this. I'm guessing what I marked in a red rectangle are the Footer Actions, I want to move the Footer Actions to the Header as illustrated in the diagram. Is that possible?
No description
awcodes
awcodes10mo ago
I would encourage you to keep the form actions. Removing them will break the html functionality users expect by being able to submit a form with the enter key. But you can also add them to the header. And call them directly since they are registered by keeping them there. For example in the getHeaderActions() method on your CreateRecord page.
Action::make('custom_create')
->label(__('Create'))
->button()
->action('create');
Action::make('custom_create')
->label(__('Create'))
->button()
->action('create');
And you can do the same for the edit page too. The action on that page is ‘save’ In my apps a create these as action classes so I can include them on any page and they will work.
Want results from more Discord servers?
Add your server