create records view. How add another custom button
In my create product view, I want to add a custom button "Generate product variants", when my own custom function to handle that button click.
How can I do that?
35 Replies
You'd need to overwrite the action (or use
->after()
) and add an action via ->modalFooterActions()
Can you please share the documentation links for overwrite the action or
->after()
. I am not sure which one it isand you meant
extraModalFooterActions
, right? https://filamentphp.com/docs/3.x/actions/modals#adding-an-extra-modal-action-button-to-the-footerIm going to need more help with this. Where do you write this code? In the resource file or one of the page files?
https://filamentphp.com/docs/3.x/actions/modals#adding-an-extra-modal-action-button-to-the-footer
The CreateAction is a in getHeaderActions on the ListPage
Actually. I dont get how this should work.
See screenshot. With or without that code block, I see the same view, with the same "create another" button
On your CreateProducts.php you can add the function
https://filamentphp.com/docs/3.x/panels/resources/creating-records#custom-actions
Here the show how to extend the default buttons
How do I keep the cancel button?
I think $this->getCancelFormAction(),
Sorry, thought it's a CreatAction not a page
I finally got something like this
Now in my
generateVariants
method, if one of the fields doesnt match a certain format, how can I return validation error?I think that is just a validation on your form itself right?
What do you mean by "my form"?
Currently in the ProductResource
if that answers your q
I bothers me that the handler function
generateVariants(): void
returns void. So this approach is a dead end if I want validation?Can you tell a bit what you are exactly trying to acchieve with the function?
You know in a typical eCommerce website, when shopkeeper enters a new product (with variants) in the backoffice,
he enters the product name, desc...
then he write out the product variant's attributes and values,
then he clicks Generate Product Variants button,
then after the backend generates all the variants, the shopkeeper can go in and fill in all the skus, prices, images, ... for all the variants,
then he clicks save, and then the product and its variants are saved to the DB
That's my intention ^
Too much customization for Filament to handle?
Maybe I have to consider using beforeCreate and afterCreate again.
Shopkeeper can generate the variants and save product and variants in the create phase.
Then shopkeeper can then fill in the variant info in the edit phase.
So unless someone have a better idea, that's my plan B
You should use
$this->form->getState()
and not access $this->data
directly.
Have a look at the default save method and copy what you need
That's a limitation you set yourself, right? Because the method was not defined beforeTrue. It doesnt have to be void
Good idea
Good idea. But where is this method?
Should I dig for it in the
vendors
folder?
Actually I cant find it.
The closest I found is this, which isnt much
When you have a good editor or IDE you usually can do a "Symbol search". Open the create page and search for the
create
method.
It's on the Filament\Resources\Pages\CreateRecord
.Thank you, that really helped
A follow up Q pls. How can you do something like this?
CreatePage
Very helpful. Thank you. But
getCreateFormAction
gets the default save button (wouldnt it?). The "Generate variants" button in my screenshot is a custom button (and event handler function) that I wrote, like this
Shouldn’t it be $this->generateVarients()? Unless the method is a registered action it won’t be available by name. So it needs to be an actual callback.
Not sure if I understood you correctly. But you mean I should disable the "Generate Variant" button in the
public function generateVariants()
method?
But how do I get a handle on the "Generate Variant" button? I cant use parent::getCreateFormAction()
to grab the buttonWell, your code doesn’t show anything about the default create action? So not sure I can answer your question. Seems like you are trying to chain onto the create action when an observer / job would make more sense.
But you can override the default save action if you choose to do so. Just make sure you also handle the save logic implicitly.
I am not using
getCreateFormAction
. Im not planning to use getCreateFormAction
, because what Im planning to do is very custom. Both of the first 2 buttons are custom.
This idea looks nice, I just wish to know how to apply it to my case
Heya. My question is quite simple.
In case you misunderstood my question to be something more complicated, I'll ask another way:
If this is how you disable the default save button getCreateFormAction()->disabled(true)
, then how can you disable a custom button?You provide a callback function to the ->disabled() method.
The code you have been sharing doesn’t align to the questions you are asking. So it’s difficult for us to give you an answer.
Ok. I hope this helps
generateVariants isn't registered as an action so you can't call it by name.
`
So
$this->action($this->generateVariants())
this is the only way to "get a handle" on my custom generate variants button?
did I understand you right?Did you try it? Does it even work?
Actually no. I looked at my code again. There is no place to put such codes. Im open to other ideas
I think like this?
This is just a syntax change
But on a positive note, I finally figured out where and how to disable custom buttons
Thank you. I finally figured out how to apply this. It worked
In
CreateProduct
page, I did
In the ProductResource
I did
Did not even know that was possible lol