-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Fields returned now in the correct table order + prepend id
- Loading branch information
1 parent
84c06cf
commit 64d774a
Showing
1 changed file
with
37 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,21 @@ | |
|
||
namespace XtendPackages\RESTPresenter\StarterKits\Filament\Base; | ||
|
||
use Filament\Forms\ComponentContainer; | ||
use Filament\Forms\Components\Field; | ||
use Filament\Resources\Pages\ListRecords; | ||
use Filament\Tables\Contracts\HasTable; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Schema; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Finder\SplFileInfo; | ||
use XtendPackages\RESTPresenter\Base\StarterKit; | ||
use XtendPackages\RESTPresenter\Concerns\InteractsWithDbSchema; | ||
|
||
class FilamentStarterKit extends StarterKit | ||
{ | ||
use InteractsWithDbSchema; | ||
|
||
protected array $resources = []; | ||
|
||
public function autoDiscover(): array | ||
|
@@ -31,20 +38,45 @@ protected function autoDiscoverResources(): void | |
$table = $page->table( | ||
table: mock('Filament\Tables\Table')->makePartial(), | ||
); | ||
|
||
/** @var \Filament\Forms\Form $form */ | ||
$form = $page->form( | ||
form: mock('Filament\Forms\Form')->makePartial(), | ||
); | ||
// $form = $page->form( | ||
// form: mock('Filament\Forms\Form')->makePartial(), | ||
// ); | ||
|
||
$resourceNamespace = Str::of(get_class($page)) | ||
Check failure on line 47 in src/StarterKits/Filament/Base/FilamentStarterKit.php
|
||
->replace('App\\Filament\\', '') | ||
->before('\\Pages') | ||
->replaceLast('Resource', '') | ||
->plural() | ||
->value(); | ||
|
||
// $fields = collect($form->getComponents()) | ||
// ->filter(fn ($field) => is_subclass_of($field, Field::class)) | ||
// ->map(fn ($field) => $field->getName()); | ||
// @todo Merge validation rules from nested form fields | ||
|
||
$modelFields = $this->generateModelFields(resolve($page->getModel()))->keyBy('name'); | ||
$tableColumns = collect(['id' => 'integer'])->merge(collect($table->getColumns()))->keys()->values(); | ||
$fields = $tableColumns->intersect($modelFields->keys())->mapWithKeys( | ||
fn ($column) => [$column => $modelFields[$column]], | ||
)->merge($modelFields); | ||
|
||
$this->resources[$resourceNamespace] = [ | ||
'columns' => collect($table->getColumns())->keys(), | ||
'fields' => $form->getComponents(), | ||
'fields' => $fields->toArray(), | ||
'model' => $page->getModel(), | ||
]; | ||
}); | ||
} | ||
|
||
protected function generateModelFields(Model $model): Collection | ||
{ | ||
$table = $model->getTable(); | ||
|
||
return $this->getTableColumns( | ||
table: $table, | ||
withProperties: true, | ||
); | ||
} | ||
} |