Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 31 additions & 11 deletions src/Filament/Integration/Builders/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,30 @@
use Filament\Schemas\Components\Grid;
use Illuminate\Support\Collection;
use Relaticle\CustomFields\Filament\Integration\Factories\FieldComponentFactory;
use Relaticle\CustomFields\Filament\Integration\Factories\SectionComponentFactory;
use Relaticle\CustomFields\Models\CustomField;
use Relaticle\CustomFields\Models\CustomFieldSection;

class FormBuilder extends BaseBuilder
{
private bool $withoutSections = false;

public function build(): Grid
{
return FormContainer::make()
->forModel($this->explicitModel ?? null)
->withoutSections($this->withoutSections)
->only($this->only)
->except($this->except);
}

public function withoutSections(bool $withoutSections = true): static
{
$this->withoutSections = $withoutSections;

return $this;
}

private function getDependentFieldCodes(Collection $fields): array
{
$dependentCodes = [];
Expand All @@ -40,21 +52,29 @@ private function getDependentFieldCodes(Collection $fields): array
public function values(): Collection
{
$fieldComponentFactory = app(FieldComponentFactory::class);
$sectionComponentFactory = app(SectionComponentFactory::class);

$allFields = $this->getFilteredSections()->flatMap(fn (mixed $section) => $section->fields);
$dependentFieldCodes = $this->getDependentFieldCodes($allFields);

// Return fields directly without Section/Fieldset wrappers
// This ensures the flat structure: custom_fields.{field_code}
// Note: We skip section grouping to avoid nested paths like custom_fields.{section_code}.{field_code}
// which causes issues with Filament v4's child schema nesting behavior.
// Visual grouping can be added later using alternative methods if needed.
return $allFields->map(
fn (CustomField $customField) => $fieldComponentFactory->create(
$customField,
$dependentFieldCodes,
$allFields
)
$createField = fn (CustomField $customField) => $fieldComponentFactory->create(
$customField,
$dependentFieldCodes,
$allFields
);

if ($this->withoutSections) {
return $allFields->map($createField);
}

return $this->getFilteredSections()
->map(function (CustomFieldSection $section) use ($sectionComponentFactory, $createField) {
$fields = $section->fields->map($createField);

return $fields->isEmpty()
? null
: $sectionComponentFactory->create($section)->schema($fields->toArray());
})
->filter();
}
}
10 changes: 10 additions & 0 deletions src/Filament/Integration/Builders/FormContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ final class FormContainer extends Grid

private array $only = [];

private bool $withoutSections = false;

public static function make(array|int|null $columns = 12): static
{
$container = new self($columns);
Expand Down Expand Up @@ -44,6 +46,13 @@ public function only(array $fieldCodes): static
return $this;
}

public function withoutSections(bool $withoutSections = true): static
{
$this->withoutSections = $withoutSections;

return $this;
}

private function generateSchema(): array
{
// Inline priority: explicit ?? record ?? model class
Expand All @@ -57,6 +66,7 @@ private function generateSchema(): array

return $builder
->forModel($model)
->withoutSections($this->withoutSections)
->only($this->only)
->except($this->except)
->values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset
{
return match ($customFieldSection->type) {
CustomFieldSectionType::SECTION => Section::make($customFieldSection->name)
->columnSpanFull()
->description($customFieldSection->description)
->columns(12),
CustomFieldSectionType::FIELDSET => Fieldset::make('custom_fields.'.$customFieldSection->code)
->columnSpanFull()
->label($customFieldSection->name)
->columns(12),
CustomFieldSectionType::HEADLESS => Grid::make(12),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset
{
return match ($customFieldSection->type) {
CustomFieldSectionType::SECTION => Section::make($customFieldSection->name)
->columnSpanFull()
->columns(12)
->description($customFieldSection->description),

CustomFieldSectionType::FIELDSET => Fieldset::make($customFieldSection->name)
->columnSpanFull()
->columns(12),

CustomFieldSectionType::HEADLESS => Grid::make($customFieldSection->column_span ?? 12),
Expand Down