Skip to content

Commit 39ac5d8

Browse files
first steps for field adaptors
1 parent a139c86 commit 39ac5d8

30 files changed

+805
-228
lines changed

config/fields/email.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<?php
22

33
use Kirby\Toolkit\I18n;
4+
use Kirby\Form\Field\EmailField;
45

56
return [
67
'extends' => 'text',
8+
'proxy' => fn(...$args) => EmailField::factory($args),
79
'props' => [
810
/**
911
* Unset inherited props
@@ -29,7 +31,7 @@
2931
* Custom placeholder text, when the field is empty.
3032
*/
3133
'placeholder' => function ($value = null) {
32-
return I18n::translate($value, $value) ?? I18n::translate('email.placeholder');
34+
return $value;
3335
}
3436
],
3537
'validations' => [

config/fields/info.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22

3-
use Kirby\Toolkit\I18n;
3+
use Kirby\Form\Field\InfoField;
44

55
return [
6+
'proxy' => function (...$attrs) {
7+
return InfoField::factory($attrs);
8+
},
69
'props' => [
710
/**
811
* Unset inherited props
@@ -20,7 +23,7 @@
2023
* Text to be displayed
2124
*/
2225
'text' => function ($value = null) {
23-
return I18n::translate($value, $value);
26+
return $value;
2427
},
2528

2629
/**
@@ -30,14 +33,5 @@
3033
return $theme;
3134
}
3235
],
33-
'computed' => [
34-
'text' => function () {
35-
if ($text = $this->text) {
36-
$text = $this->model()->toSafeString($text);
37-
$text = $this->kirby()->kirbytext($text);
38-
return $text;
39-
}
40-
}
41-
],
4236
'save' => false,
4337
];

config/fields/structure.php

Lines changed: 9 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
<?php
22

3-
use Kirby\Data\Data;
4-
use Kirby\Exception\InvalidArgumentException;
5-
use Kirby\Form\Form;
6-
use Kirby\Toolkit\A;
7-
use Kirby\Toolkit\I18n;
8-
use Kirby\Toolkit\Str;
3+
use Kirby\Form\Field\StructureField;
94

105
return [
116
'mixins' => ['min'],
7+
'proxy' => function (...$attrs) {
8+
return StructureField::factory($attrs);
9+
},
1210
'props' => [
1311
/**
1412
* Unset inherited props
@@ -30,9 +28,7 @@
3028
* Optional columns definition to only show selected fields in the structure table.
3129
*/
3230
'columns' => function (array $columns = []) {
33-
// lower case all keys, because field names will
34-
// be lowercase as well.
35-
return array_change_key_case($columns);
31+
return $columns;
3632
},
3733

3834
/**
@@ -46,7 +42,7 @@
4642
* The placeholder text if no items have been added yet
4743
*/
4844
'empty' => function ($empty = null) {
49-
return I18n::translate($empty, $empty);
45+
return $empty;
5046
},
5147

5248
/**
@@ -101,146 +97,16 @@
10197
}
10298
],
10399
'computed' => [
104-
'default' => function () {
105-
return $this->rows($this->default);
106-
},
107100
'value' => function () {
108-
return $this->rows($this->value);
109-
},
110-
'fields' => function () {
111-
if (empty($this->fields) === true) {
112-
return [];
113-
}
114-
115-
return $this->form()->fields()->toProps();
116-
},
117-
'columns' => function () {
118-
$columns = [];
119-
$blueprint = $this->columns;
120-
121-
// if no custom columns have been defined,
122-
// gather all fields as columns
123-
if (empty($blueprint) === true) {
124-
// skip hidden fields
125-
$fields = array_filter(
126-
$this->fields,
127-
fn ($field) =>
128-
$field['type'] !== 'hidden' && $field['hidden'] !== true
129-
);
130-
$fields = array_column($fields, 'name');
131-
$blueprint = array_fill_keys($fields, true);
132-
}
133-
134-
foreach ($blueprint as $name => $column) {
135-
$field = $this->fields[$name] ?? null;
136-
137-
// Skip empty and unsaveable fields
138-
// They should never be included as column
139-
if (
140-
empty($field) === true ||
141-
$field['saveable'] === false
142-
) {
143-
continue;
144-
}
145-
146-
if (is_array($column) === false) {
147-
$column = [];
148-
}
149-
150-
$column['type'] ??= $field['type'];
151-
$column['label'] ??= $field['label'] ?? $name;
152-
$column['label'] = I18n::translate($column['label'], $column['label']);
153-
154-
$columns[$name] = $column;
155-
}
156-
157-
// make the first column visible on mobile
158-
// if no other mobile columns are defined
159-
if (in_array(true, array_column($columns, 'mobile'), true) === false) {
160-
$columns[array_key_first($columns)]['mobile'] = true;
161-
}
162-
163-
return $columns;
164-
}
165-
],
166-
'methods' => [
167-
'rows' => function ($value) {
168-
$rows = Data::decode($value, 'yaml');
169-
$value = [];
170-
171-
foreach ($rows as $index => $row) {
172-
if (is_array($row) === false) {
173-
continue;
174-
}
175-
176-
$value[] = $this->form()->fill(input: $row, passthrough: true)->toFormValues();
177-
}
178-
179-
return $value;
101+
return $this->proxy->fill($this->value ?? [])->toFormValue();
180102
},
181-
'form' => function () {
182-
$this->form ??= new Form(
183-
fields: $this->attrs['fields'] ?? [],
184-
model: $this->model,
185-
language: 'current'
186-
);
187-
188-
return $this->form->reset();
189-
}
190103
],
191104
'save' => function ($value) {
192-
$data = [];
193-
$form = $this->form();
194-
$defaults = $form->defaults();
195-
196-
foreach ($value as $index => $row) {
197-
$row = $form
198-
->reset()
199-
->fill(
200-
input: $defaults,
201-
)
202-
->submit(
203-
input: $row,
204-
passthrough: true
205-
)
206-
->toStoredValues();
207-
208-
// remove frontend helper id
209-
unset($row['_id']);
210-
211-
$data[] = $row;
212-
}
213-
214-
return $data;
105+
return $this->proxy->submit($value)->toStoredValue();
215106
},
216107
'validations' => [
217108
'min',
218109
'max',
219-
'structure' => function ($value) {
220-
if (empty($value) === true) {
221-
return true;
222-
}
223-
224-
$values = A::wrap($value);
225-
226-
foreach ($values as $index => $value) {
227-
$form = $this->form();
228-
$form->fill(input: $value);
229-
230-
foreach ($form->fields() as $field) {
231-
$errors = $field->errors();
232-
233-
if (empty($errors) === false) {
234-
throw new InvalidArgumentException(
235-
key: 'structure.validation',
236-
data: [
237-
'field' => $field->label() ?? Str::ucfirst($field->name()),
238-
'index' => $index + 1
239-
]
240-
);
241-
}
242-
}
243-
}
244-
}
110+
'structure' => fn ($value) => $this->proxy->validations()['structure']($value)
245111
]
246112
];

config/fields/tel.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22

3+
use Kirby\Form\Field\TelField;
4+
35
return [
46
'extends' => 'text',
7+
'proxy' => fn(...$args) => TelField::factory($args),
58
'props' => [
69
/**
710
* Unset inherited props

config/fields/text.php

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<?php
22

33
use Kirby\Exception\InvalidArgumentException;
4-
use Kirby\Toolkit\Str;
4+
use Kirby\Form\Field\TextField;
55

66
return [
7+
'proxy' => fn(...$args) => TextField::factory($args),
78
'props' => [
89

910
/**
@@ -34,7 +35,7 @@
3435
* Sets the font family (sans or monospace)
3536
*/
3637
'font' => function (string|null $font = null) {
37-
return $font === 'monospace' ? 'monospace' : 'sans-serif';
38+
return $font;
3839
},
3940

4041
/**
@@ -66,44 +67,10 @@
6667
},
6768
],
6869
'computed' => [
69-
'default' => function () {
70-
return $this->convert($this->default);
71-
},
7270
'value' => function () {
7371
return (string)$this->convert($this->value);
7472
}
7573
],
76-
'methods' => [
77-
'convert' => function ($value) {
78-
if ($this->converter() === null) {
79-
return $value;
80-
}
81-
82-
$converter = $this->converters()[$this->converter()];
83-
84-
if (is_array($value) === true) {
85-
return array_map($converter, $value);
86-
}
87-
88-
return call_user_func($converter, trim($value ?? ''));
89-
},
90-
'converters' => function (): array {
91-
return [
92-
'lower' => function ($value) {
93-
return Str::lower($value);
94-
},
95-
'slug' => function ($value) {
96-
return Str::slug($value);
97-
},
98-
'ucfirst' => function ($value) {
99-
return Str::ucfirst($value);
100-
},
101-
'upper' => function ($value) {
102-
return Str::upper($value);
103-
},
104-
];
105-
},
106-
],
10774
'validations' => [
10875
'minlength',
10976
'maxlength',

config/fields/url.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php
22

3-
use Kirby\Toolkit\I18n;
3+
use Kirby\Form\Field\UrlField;
44

55
return [
6+
'proxy' => fn(...$args) => UrlField::factory($args),
67
'extends' => 'text',
78
'props' => [
89
/**
@@ -31,7 +32,7 @@
3132
* Sets custom placeholder text, when the field is empty
3233
*/
3334
'placeholder' => function ($value = null) {
34-
return I18n::translate($value, $value) ?? 'https://example.com';
35+
return $value;
3536
}
3637
],
3738
'validations' => [

0 commit comments

Comments
 (0)