-
-
Notifications
You must be signed in to change notification settings - Fork 632
Expand file tree
/
Copy pathAbstractJsDriver.php
More file actions
187 lines (165 loc) · 4.31 KB
/
Copy pathAbstractJsDriver.php
File metadata and controls
187 lines (165 loc) · 4.31 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace Statamic\Forms\JsDrivers;
use Illuminate\Support\Collection;
use Statamic\Forms\Form;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Tags\Parameters;
abstract class AbstractJsDriver implements JsDriver
{
protected $form;
protected $options;
protected $params;
/**
* Instantiate JS driver.
*
* @param array $options
*/
public function __construct(Form $form, $options = [], ?Parameters $params = null)
{
$this->form = $form;
$this->options = $options;
$this->params = $params;
if (method_exists($this, 'parseOptions')) {
$this->parseOptions($options);
}
$this->validateRenderMethodReturnsHtml();
}
/**
* Add to form view data.
*
* @param array $data
* @return array
*/
public function addToFormData($data)
{
return [];
}
/**
* Add to form html tag attributes.
*
* @return array
*/
public function addToFormAttributes()
{
return [];
}
/**
* Add to renderable field view data.
*
* @param \Statamic\Fields\Field $field
* @param array $data
* @return array
*/
public function addToRenderableFieldData($field, $data)
{
return [];
}
/**
* Add to renderable field html tag attributes.
*
* @param \Statamic\Fields\Field $field
* @return array
*/
public function addToRenderableFieldAttributes($field)
{
return [];
}
/**
* Add to renderable page view data.
*
* @param \Statamic\Fields\Tab $page
* @param array $data
* @return array
*/
public function addToRenderablePageData($page, $data)
{
return [];
}
/**
* Render form html.
*
* @param string $html
* @return string
*/
public function render($html)
{
return $html;
}
/**
* Copy renderable `show_field` JS from each individual field for hardcoding field html using top-level form data.
*/
public function copyShowFieldToFormData(array $fields): array
{
return $this
->flattenFields($fields)
->each(fn ($field) => $this->validateShowFieldDefined($field))
->pluck('show_field', 'handle')
->all();
}
/**
* Validate that `show_field` is defined in `addToRenderableFieldData()` output.
*
* @throws \Exception
*/
protected function validateShowFieldDefined(array $field): void
{
if (! isset($field['show_field'])) {
throw new \Exception('JS driver requires [show_field] to be defined in [addToRenderableFieldData()] output!');
}
}
/**
* Validate that render method returns `$html` var.
*
* @throws \Exception
*/
protected function validateRenderMethodReturnsHtml()
{
if (! Str::contains($this->render('<VALIDATING-HTML />'), '<VALIDATING-HTML />')) {
throw new \Exception('JS driver requires [$html] to be returned in [render()] output!');
}
}
/**
* Get initial form data.
*/
protected function getInitialFormData(): array
{
return $this->form
->blueprint()
->fields()
->addValues(old() ?? [])
->preProcess()
->values()
->when($this->form->honeypot(), fn ($fields, $honeypot) => $fields->merge([$honeypot => null]))
->all();
}
/**
* Recursively get flattened fields collection.
*/
protected function flattenFields(array|Collection $fields): Collection
{
return collect($fields)->flatMap(fn ($field) => [
$field,
...$this->flattenFields(Arr::get($field, 'fields') ?? [])->all(),
]);
}
/**
* Get JS driver handle from class name.
*/
public static function handle(): string
{
$className = collect(explode('\\', static::class))->last();
return Str::snake($className);
}
/**
* Register driver with Statamic.
*/
public static function register(): void
{
if (! app()->has('statamic.form-js-drivers')) {
return;
}
$handle = static::handle();
app('statamic.form-js-drivers')[$handle] = static::class;
}
}