-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathClosureFormAdapter.php
More file actions
93 lines (79 loc) · 3.22 KB
/
ClosureFormAdapter.php
File metadata and controls
93 lines (79 loc) · 3.22 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
<?php
declare(strict_types=1);
namespace Relaticle\CustomFields\Filament\Integration\Components\Forms;
use Closure;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\Select;
use Relaticle\CustomFields\Filament\Integration\Base\AbstractFormComponent;
use Relaticle\CustomFields\Filament\Integration\Concerns\Forms\ConfiguresColorOptions;
use Relaticle\CustomFields\Models\CustomField;
use Relaticle\CustomFields\Services\ValidationService;
use Relaticle\CustomFields\Services\Visibility\CoreVisibilityLogicService;
use Relaticle\CustomFields\Services\Visibility\FrontendVisibilityService;
/**
* Simple adapter that allows Closures to benefit from AbstractFormComponent's
* full feature set (validation, visibility, state management, etc.) while
* maintaining user configuration priority.
*
* This adapter extends AbstractFormComponent and simply implements the create()
* method to call the user's Closure, then lets AbstractFormComponent handle
* all the complex configuration logic.
*/
final readonly class ClosureFormAdapter extends AbstractFormComponent
{
use ConfiguresColorOptions;
public function __construct(
private Closure $closure,
ValidationService $validationService,
CoreVisibilityLogicService $coreVisibilityLogic,
FrontendVisibilityService $frontendVisibilityService,
) {
parent::__construct($validationService, $coreVisibilityLogic, $frontendVisibilityService);
}
/**
* Implementation of AbstractFormComponent's create() method.
* Calls the user's Closure and automatically applies built-in features.
*/
public function create(CustomField $customField): Field
{
$field = ($this->closure)($customField);
// Apply built-in features automatically for choice fields
if ($customField->isChoiceField() && ! $customField->typeData->withoutUserOptions) {
$field = $this->applyUserDefinedOptions($field, $customField);
}
// Apply color options if enabled
if ($this->hasColorOptionsEnabled($customField)) {
return $this->applyColorOptions($field, $customField);
}
return $field;
}
/**
* Apply user-defined options to choice fields automatically.
*/
private function applyUserDefinedOptions(Field $field, CustomField $customField): Field
{
if (method_exists($field, 'options')) {
// Only apply if field doesn't already have options configured
$existingOptions = method_exists($field, 'getOptions') ? $field->getOptions() : null;
if (empty($existingOptions)) {
$options = $this->getCustomFieldOptions($customField);
$field->options($options);
}
}
return $field;
}
/**
* Apply color options based on field type.
*/
private function applyColorOptions(Field $field, CustomField $customField): Field
{
if (method_exists($field, 'options') && $field instanceof Select) {
$coloredOptions = $this->getSelectColoredOptions($customField);
return $field
->native(false)
->allowHtml()
->options($coloredOptions);
}
return $field;
}
}