How can I add fields to / extend a resource from a plugin
I want to add a form field to an existing module/plugin we are using. I already tried extending the class and overwriting the form method but thats not working. Any idea how this could be done?
12 Replies
The plugins needs to provide the option. Otherwise you need to extend the resouce and add your classes, but I guess it's easier to just copy over stuff.
Thanks! How do I provide this option?
I am not sure what you mean by providing the option by the plugin, is there an option which alows extending?
It's a module/plugin (the resource) we are using in multiple projects. But in one project we need 1 extra field for that resource. So I need to add it in the project but not in the module. So I extended the moduled resource and made the project use the custom one that extended the module. Here I would like to overwrite the form function or add the field.
You could define a method like
getFormSchema
that returns the schema as an array which is used in form()
then. That one could be extended and you could add fields before or after.Do you have an example? I am having trouble firing a non static getFormSchema in the static form function.
Well it should be static then?!
I thought static functions could not be extended?
Trying it with static:: but no luck unfortunally
class ResourceCompanyResource extends Resource
{
public static function form(Form $form): Form
{
return $form
->schema(static::getFormSchema())->columns(1);
}
public static function getFormSchema(): array
{
return [
Tabs::make()
->tabs([
Tab::make('Information')
->schema(static::getInformationTabSchema()),
Tab::make('Invoice')
->schema(static::getInvoiceTabSchema()),
Tab::make('Financial')
->schema(static::getFinancialTabSchema())
])
];
}
}
class CompanyResource extends ResourcesCompanyResource
{
public static function getFormSchema(): array
{
return [
Tabs::make()
->tabs([
Tab::make('Information2')
->schema(static::getInformationTabSchema()),
Tab::make('Invoice')
->schema(static::getInvoiceTabSchema()),
Tab::make('Financial')
->schema(static::getFinancialTabSchema())
])
];
}
}
What’s the issue? Are the resource pages actually using the extended resource?
Im trying to make Information2 overwrite Information but its still showing Information
The CompanyResource is actually extending ResourcesCompanyResource
Should it work like this you think?
But the Create and EditPage? Do you have separate ones for the second resource?
No, I don't. Only for the one that is getting extended
Solution
I made them, now it works!
Thanks mate