-
Notifications
You must be signed in to change notification settings - Fork 16
Feature/auto detect model context #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Relaticle\CustomFields\Filament\Integration\Builders; | ||
|
||
use Filament\Schemas\Components\Grid; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
final class FormContainer extends Grid | ||
{ | ||
private Model|string|null $explicitModel = null; | ||
|
||
private array $except = []; | ||
|
||
private array $only = []; | ||
|
||
public static function make(array|int|null $columns = 1): static | ||
{ | ||
$container = new self($columns); | ||
|
||
// Defer schema generation until component is in container | ||
$container->schema(fn (): array => $container->generateSchema()); | ||
|
||
return $container; | ||
} | ||
|
||
public function forModel(Model|string|null $model): static | ||
{ | ||
$this->explicitModel = $model; | ||
|
||
return $this; | ||
} | ||
|
||
public function except(array $fieldCodes): static | ||
{ | ||
$this->except = $fieldCodes; | ||
|
||
return $this; | ||
} | ||
|
||
public function only(array $fieldCodes): static | ||
{ | ||
$this->only = $fieldCodes; | ||
|
||
return $this; | ||
} | ||
|
||
private function generateSchema(): array | ||
{ | ||
// Inline priority: explicit ?? record ?? model class | ||
$model = $this->explicitModel ?? $this->getRecord() ?? $this->getModel(); | ||
|
||
if ($model === null) { | ||
return []; // Graceful fallback | ||
} | ||
|
||
$builder = app(FormBuilder::class); | ||
|
||
return $builder | ||
->forModel($model) | ||
->only($this->only) | ||
->except($this->except) | ||
->values() | ||
->toArray(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<?php | ||
|
||
namespace Relaticle\CustomFields\Filament\Integration\Builders; | ||
|
||
use Filament\Forms\Components\Field; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This import appears to be incorrect for an infolist component. InfolistContainer should import Entry components from Filament\Infolists\Components\Entry instead of Field from Forms. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
use Filament\Schemas\Components\Grid; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
final class InfolistContainer extends Grid | ||
{ | ||
private Model|string|null $explicitModel = null; | ||
|
||
private array $except = []; | ||
|
||
private array $only = []; | ||
|
||
private bool $hiddenLabels = false; | ||
|
||
private bool $visibleWhenFilled = false; | ||
|
||
private bool $withoutSections = false; | ||
|
||
public static function make(array|int|null $columns = 1): static | ||
{ | ||
$container = new self($columns); | ||
|
||
// Defer schema generation until component is in container | ||
$container->schema(fn (): array => $container->generateSchema()); | ||
|
||
return $container; | ||
} | ||
|
||
public function forModel(Model|string|null $model): static | ||
{ | ||
$this->explicitModel = $model; | ||
|
||
return $this; | ||
} | ||
|
||
public function except(array $fieldCodes): static | ||
{ | ||
$this->except = $fieldCodes; | ||
|
||
return $this; | ||
} | ||
|
||
public function only(array $fieldCodes): static | ||
{ | ||
$this->only = $fieldCodes; | ||
|
||
return $this; | ||
} | ||
|
||
public function hiddenLabels(bool $hiddenLabels = true): static | ||
{ | ||
$this->hiddenLabels = $hiddenLabels; | ||
|
||
return $this; | ||
} | ||
|
||
public function visibleWhenFilled(bool $visibleWhenFilled = true): static | ||
{ | ||
$this->visibleWhenFilled = $visibleWhenFilled; | ||
|
||
return $this; | ||
} | ||
|
||
public function withoutSections(bool $withoutSections = true): static | ||
{ | ||
$this->withoutSections = $withoutSections; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @return array<int, Field> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The return type annotation is incorrect. Since this is for infolists, it should return array<int, Entry> instead of array<int, Field>. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||
*/ | ||
private function generateSchema(): array | ||
{ | ||
// Inline priority: explicit ?? record ?? model class | ||
$model = $this->explicitModel ?? $this->getRecord() ?? $this->getModel(); | ||
|
||
if ($model === null) { | ||
return []; // Graceful fallback | ||
} | ||
|
||
$builder = app(InfolistBuilder::class) | ||
->forModel($model) | ||
->only($this->only) | ||
->except($this->except) | ||
->hiddenLabels($this->hiddenLabels) | ||
->visibleWhenFilled($this->visibleWhenFilled) | ||
->withoutSections($this->withoutSections); | ||
|
||
return $builder->values()->toArray(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding an optional parameter to an existing public method changes the API signature. This could be a breaking change if other code expects the original signature. Consider creating a separate method or ensuring backward compatibility.
Copilot uses AI. Check for mistakes.