-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathFormBuilder.php
More file actions
96 lines (77 loc) · 3.33 KB
/
FormBuilder.php
File metadata and controls
96 lines (77 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
// ABOUTME: Builder for creating Filament form schemas from custom fields
// ABOUTME: Handles form generation with sections, validation, and field dependencies
namespace Relaticle\CustomFields\Filament\Integration\Builders;
use Filament\Schemas\Components\Grid;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Relaticle\CustomFields\Enums\CustomFieldsFeature;
use Relaticle\CustomFields\FeatureSystem\FeatureManager;
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 = null;
public function build(): Grid
{
$container = FormContainer::make()
->forModel($this->explicitModel ?? null)
->only($this->only)
->except($this->except);
// Only set withoutSections if explicitly configured
if ($this->withoutSections !== null) {
$container->withoutSections($this->withoutSections);
}
return $container;
}
public function withoutSections(bool $withoutSections = true): static
{
$this->withoutSections = $withoutSections;
return $this;
}
private function getDependentFieldCodes(Collection $fields): array
{
$dependentCodes = [];
foreach ($fields as $field) {
if ($field->visibility_conditions && is_array($field->visibility_conditions)) {
foreach ($field->visibility_conditions as $condition) {
if (isset($condition['field'])) {
$dependentCodes[] = $condition['field'];
}
}
}
}
return array_unique($dependentCodes);
}
public function values(): Collection
{
$fieldComponentFactory = app(FieldComponentFactory::class);
$sectionComponentFactory = app(SectionComponentFactory::class);
// Use getAllFields() which handles sectionless mode properly
$allFields = $this->getAllFields();
$dependentFieldCodes = $this->getDependentFieldCodes($allFields);
$createField = fn (CustomField $customField) => $fieldComponentFactory->create(
$customField,
$dependentFieldCodes,
$allFields
);
// Return flat fields if sections are disabled or withoutSections is set
$sectionsDisabled = ! FeatureManager::isEnabled(CustomFieldsFeature::SYSTEM_SECTIONS_ENABLED);
if ($this->withoutSections || $sectionsDisabled) {
return $allFields->map($createField);
}
$record = $this->explicitModel instanceof Model && $this->explicitModel->exists
? $this->explicitModel
: null;
return $this->getFilteredSections()
->map(function (CustomFieldSection $section) use ($sectionComponentFactory, $createField, $allFields, $record) {
$fields = $section->fields->map($createField);
return $fields->isEmpty()
? null
: $sectionComponentFactory->create($section, $allFields, $record)->schema($fields->toArray());
})
->filter();
}
}