A quick overview of how Modules work.
In this system, a module is a set of pre-defined CRUD details. For example, a module can contain more information for a User
CRUD.
This module will have the following details:
- Unlimited amount of CRUDs inside
- Each CRUD will have its own details
- Each CRUD will have its own fields
- Each field will have its own details
These modules are quickly installed using app/Interfaces/ModuleBase.php
methods install()
and uninstall()
.
Each module, if needed, can override those methods.
To create a module, you have a few options:
- Start from scratch
- Copy one of the existing modules and modify it
In both cases, the result should be the same:
- A file in the
systems/generators/filament3/src/Modules
folder - A-line with unique
slug
insystems/generators/filament3/src/Modules/ModuleManager.php.
Now, each module should have an implementation of the getCruds()
method. This method should return an array of CRUDs. For example, take from BaseModule
:
public function getCruds(): array
{
return [
(new Crud([
'type' => CrudTypes::PARENT,
'title' => str('User Management')->singular()->studly(),
'visual_title' => 'User Management',
'icon' => 'heroicon-o-users',
'menu_order' => 1,
'is_hidden' => false,
'module_crud' => true,
'system' => true,
])),
(new Crud([
'parent_id' => str('User Management')->singular()->studly(),
'type' => CrudTypes::CRUD,
'title' => str('Permissions')->singular()->studly(),
'visual_title' => 'Permissions',
'icon' => '',
'menu_order' => 1,
'is_hidden' => false,
'module_crud' => true,
'system' => true,
]))
->setRelation('fields', [
$this->getIDField(),
new CrudField([
'type' => CrudFieldTypes::TEXT,
'key' => str('Title')->lower()
->snake()
->toString(),
'label' => 'Title',
'validation' => 'required',
'in_list' => true,
'in_show' => true,
'in_create' => true,
'in_edit' => true,
'nullable' => false,
'tooltip' => null,
'system' => true,
'enabled' => true,
'order' => 2,
]),
$this->getCreatedAtField(3),
$this->getUpdatedAtField(4),
$this->getDeletedAtField(5),
]
),
];
}
Everything is done via new Module instances. We don't save them to the database; we use them as DTOs.
Installation/Removal is handled automatically in the system. It is done via:
ModuleService::getModuleClass(App\Models\Panel, 'module-slug-goes-here')
->install(App\Models\Panel);
And:
ModuleService::getModuleClass(App\Models\Panel, 'module-slug-goes-here')
->uninstall(App\Models\Panel);
Both methods accept a Panel
(the current admin panel) as a parameter and a module SLUG.