Skip to content

Commit 7e828d6

Browse files
Merge pull request #54 from Relaticle/fix/section-type-rendering
fix: respect section type in form builder
2 parents 50c6095 + 4992316 commit 7e828d6

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

src/Filament/Integration/Builders/FormBuilder.php

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,30 @@
88
use Filament\Schemas\Components\Grid;
99
use Illuminate\Support\Collection;
1010
use Relaticle\CustomFields\Filament\Integration\Factories\FieldComponentFactory;
11+
use Relaticle\CustomFields\Filament\Integration\Factories\SectionComponentFactory;
1112
use Relaticle\CustomFields\Models\CustomField;
13+
use Relaticle\CustomFields\Models\CustomFieldSection;
1214

1315
class FormBuilder extends BaseBuilder
1416
{
17+
private bool $withoutSections = false;
18+
1519
public function build(): Grid
1620
{
1721
return FormContainer::make()
1822
->forModel($this->explicitModel ?? null)
23+
->withoutSections($this->withoutSections)
1924
->only($this->only)
2025
->except($this->except);
2126
}
2227

28+
public function withoutSections(bool $withoutSections = true): static
29+
{
30+
$this->withoutSections = $withoutSections;
31+
32+
return $this;
33+
}
34+
2335
private function getDependentFieldCodes(Collection $fields): array
2436
{
2537
$dependentCodes = [];
@@ -40,21 +52,29 @@ private function getDependentFieldCodes(Collection $fields): array
4052
public function values(): Collection
4153
{
4254
$fieldComponentFactory = app(FieldComponentFactory::class);
55+
$sectionComponentFactory = app(SectionComponentFactory::class);
4356

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

47-
// Return fields directly without Section/Fieldset wrappers
48-
// This ensures the flat structure: custom_fields.{field_code}
49-
// Note: We skip section grouping to avoid nested paths like custom_fields.{section_code}.{field_code}
50-
// which causes issues with Filament v4's child schema nesting behavior.
51-
// Visual grouping can be added later using alternative methods if needed.
52-
return $allFields->map(
53-
fn (CustomField $customField) => $fieldComponentFactory->create(
54-
$customField,
55-
$dependentFieldCodes,
56-
$allFields
57-
)
60+
$createField = fn (CustomField $customField) => $fieldComponentFactory->create(
61+
$customField,
62+
$dependentFieldCodes,
63+
$allFields
5864
);
65+
66+
if ($this->withoutSections) {
67+
return $allFields->map($createField);
68+
}
69+
70+
return $this->getFilteredSections()
71+
->map(function (CustomFieldSection $section) use ($sectionComponentFactory, $createField) {
72+
$fields = $section->fields->map($createField);
73+
74+
return $fields->isEmpty()
75+
? null
76+
: $sectionComponentFactory->create($section)->schema($fields->toArray());
77+
})
78+
->filter();
5979
}
6080
}

src/Filament/Integration/Builders/FormContainer.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ final class FormContainer extends Grid
1313

1414
private array $only = [];
1515

16+
private bool $withoutSections = false;
17+
1618
public static function make(array|int|null $columns = 12): static
1719
{
1820
$container = new self($columns);
@@ -44,6 +46,13 @@ public function only(array $fieldCodes): static
4446
return $this;
4547
}
4648

49+
public function withoutSections(bool $withoutSections = true): static
50+
{
51+
$this->withoutSections = $withoutSections;
52+
53+
return $this;
54+
}
55+
4756
private function generateSchema(): array
4857
{
4958
// Inline priority: explicit ?? record ?? model class
@@ -57,6 +66,7 @@ private function generateSchema(): array
5766

5867
return $builder
5968
->forModel($model)
69+
->withoutSections($this->withoutSections)
6070
->only($this->only)
6171
->except($this->except)
6272
->values()

src/Filament/Integration/Factories/SectionComponentFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset
1616
{
1717
return match ($customFieldSection->type) {
1818
CustomFieldSectionType::SECTION => Section::make($customFieldSection->name)
19+
->columnSpanFull()
1920
->description($customFieldSection->description)
2021
->columns(12),
2122
CustomFieldSectionType::FIELDSET => Fieldset::make('custom_fields.'.$customFieldSection->code)
23+
->columnSpanFull()
2224
->label($customFieldSection->name)
2325
->columns(12),
2426
CustomFieldSectionType::HEADLESS => Grid::make(12),

src/Filament/Integration/Factories/SectionInfolistsFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ public function create(CustomFieldSection $customFieldSection): Section|Fieldset
1616
{
1717
return match ($customFieldSection->type) {
1818
CustomFieldSectionType::SECTION => Section::make($customFieldSection->name)
19+
->columnSpanFull()
1920
->columns(12)
2021
->description($customFieldSection->description),
2122

2223
CustomFieldSectionType::FIELDSET => Fieldset::make($customFieldSection->name)
24+
->columnSpanFull()
2325
->columns(12),
2426

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

0 commit comments

Comments
 (0)