|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Relaticle\CustomFields\Filament\Integration\Builders; |
| 4 | + |
| 5 | +use Filament\Forms\Components\Field; |
| 6 | +use Filament\Schemas\Components\Grid; |
| 7 | +use Illuminate\Database\Eloquent\Model; |
| 8 | + |
| 9 | +final class InfolistContainer extends Grid |
| 10 | +{ |
| 11 | + private Model|string|null $explicitModel = null; |
| 12 | + |
| 13 | + private array $except = []; |
| 14 | + |
| 15 | + private array $only = []; |
| 16 | + |
| 17 | + private bool $hiddenLabels = false; |
| 18 | + |
| 19 | + private bool $visibleWhenFilled = false; |
| 20 | + |
| 21 | + private bool $withoutSections = false; |
| 22 | + |
| 23 | + public static function make(array|int|null $columns = 1): static |
| 24 | + { |
| 25 | + $container = new self($columns); |
| 26 | + |
| 27 | + // Defer schema generation until component is in container |
| 28 | + $container->schema(fn (): array => $container->generateSchema()); |
| 29 | + |
| 30 | + return $container; |
| 31 | + } |
| 32 | + |
| 33 | + public function forModel(Model|string|null $model): static |
| 34 | + { |
| 35 | + $this->explicitModel = $model; |
| 36 | + |
| 37 | + return $this; |
| 38 | + } |
| 39 | + |
| 40 | + public function except(array $fieldCodes): static |
| 41 | + { |
| 42 | + $this->except = $fieldCodes; |
| 43 | + |
| 44 | + return $this; |
| 45 | + } |
| 46 | + |
| 47 | + public function only(array $fieldCodes): static |
| 48 | + { |
| 49 | + $this->only = $fieldCodes; |
| 50 | + |
| 51 | + return $this; |
| 52 | + } |
| 53 | + |
| 54 | + public function hiddenLabels(bool $hiddenLabels = true): static |
| 55 | + { |
| 56 | + $this->hiddenLabels = $hiddenLabels; |
| 57 | + |
| 58 | + return $this; |
| 59 | + } |
| 60 | + |
| 61 | + public function visibleWhenFilled(bool $visibleWhenFilled = true): static |
| 62 | + { |
| 63 | + $this->visibleWhenFilled = $visibleWhenFilled; |
| 64 | + |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + public function withoutSections(bool $withoutSections = true): static |
| 69 | + { |
| 70 | + $this->withoutSections = $withoutSections; |
| 71 | + |
| 72 | + return $this; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<int, Field> |
| 77 | + */ |
| 78 | + private function generateSchema(): array |
| 79 | + { |
| 80 | + // Inline priority: explicit ?? record ?? model class |
| 81 | + $model = $this->explicitModel ?? $this->getRecord() ?? $this->getModel(); |
| 82 | + |
| 83 | + if ($model === null) { |
| 84 | + return []; // Graceful fallback |
| 85 | + } |
| 86 | + |
| 87 | + $builder = app(InfolistBuilder::class) |
| 88 | + ->forModel($model) |
| 89 | + ->only($this->only) |
| 90 | + ->except($this->except) |
| 91 | + ->hiddenLabels($this->hiddenLabels) |
| 92 | + ->visibleWhenFilled($this->visibleWhenFilled) |
| 93 | + ->withoutSections($this->withoutSections); |
| 94 | + |
| 95 | + return $builder->values()->toArray(); |
| 96 | + } |
| 97 | +} |
0 commit comments