Skip to content

Commit 0c46f6c

Browse files
Various improvements
1 parent d01177d commit 0c46f6c

15 files changed

+552
-588
lines changed

config/fields/structure.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
}
142142

143143
$column['type'] ??= $field['type'];
144-
$column['label'] ??= $field['label'] ?? $name;
144+
$column['label'] ??= $field['label'] ?? Str::ucfirst($name);
145145
$column['label'] = I18n::translate($column['label'], $column['label']);
146146

147147
$columns[$name] = $column;

src/Form/Field.php

+39-143
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
namespace Kirby\Form;
44

55
use Closure;
6-
use Kirby\Cms\HasSiblings;
76
use Kirby\Exception\InvalidArgumentException;
87
use Kirby\Toolkit\A;
98
use Kirby\Toolkit\Component;
10-
use Kirby\Toolkit\I18n;
11-
use Kirby\Toolkit\Str;
129

1310
/**
1411
* Form Field object that takes a Vue component style
@@ -23,22 +20,15 @@
2320
*/
2421
class Field extends Component
2522
{
26-
/**
27-
* @use \Kirby\Cms\HasSiblings<\Kirby\Form\Fields>
28-
*/
29-
use HasSiblings;
30-
use Mixin\Api;
23+
use Mixin\Common;
24+
use Mixin\Endpoints;
3125
use Mixin\Model;
26+
use Mixin\Siblings;
3227
use Mixin\Translatable;
3328
use Mixin\Validation;
3429
use Mixin\When;
3530
use Mixin\Value;
3631

37-
/**
38-
* Parent collection with all fields of the current form
39-
*/
40-
public Fields $siblings;
41-
4232
/**
4333
* Registry for all component mixins
4434
*/
@@ -81,8 +71,22 @@ public function __construct(
8171

8272
parent::__construct($type, $attrs);
8373

84-
// set the siblings collection
85-
$this->siblings = $siblings ?? new Fields([$this]);
74+
$this->setSiblings($attrs['siblings'] ?? null);
75+
}
76+
77+
/**
78+
* Returns field api routes
79+
*/
80+
public function api(): array
81+
{
82+
if (
83+
isset($this->options['api']) === true &&
84+
$this->options['api'] instanceof Closure
85+
) {
86+
return $this->options['api']->call($this);
87+
}
88+
89+
return [];
8690
}
8791

8892
/**
@@ -95,38 +99,38 @@ public static function defaults(): array
9599
/**
96100
* Optional text that will be shown after the input
97101
*/
98-
'after' => function ($after = null) {
99-
return I18n::translate($after, $after);
102+
'after' => function (array|string|null $after = null) {
103+
return $this->i18n($after);
100104
},
101105
/**
102106
* Sets the focus on this field when the form loads. Only the first field with this label gets
103107
*/
104-
'autofocus' => function (bool|null $autofocus = null): bool {
105-
return $autofocus ?? false;
108+
'autofocus' => function (bool $autofocus = false): bool {
109+
return $autofocus;
106110
},
107111
/**
108112
* Optional text that will be shown before the input
109113
*/
110-
'before' => function ($before = null) {
111-
return I18n::translate($before, $before);
114+
'before' => function (array|string|null $before = null) {
115+
return $this->i18n($before);
112116
},
113117
/**
114118
* Default value for the field, which will be used when a page/file/user is created
115119
*/
116-
'default' => function ($default = null) {
120+
'default' => function (mixed $default = null) {
117121
return $default;
118122
},
119123
/**
120124
* If `true`, the field is no longer editable and will not be saved
121125
*/
122-
'disabled' => function (bool|null $disabled = null): bool {
123-
return $disabled ?? false;
126+
'disabled' => function (bool $disabled = false): bool {
127+
return $disabled;
124128
},
125129
/**
126130
* Optional help text below the field
127131
*/
128-
'help' => function ($help = null) {
129-
return I18n::translate($help, $help);
132+
'help' => function (array|string|null $help = null) {
133+
return $this->i18n($help);
130134
},
131135
/**
132136
* Optional icon that will be shown at the end of the field
@@ -137,20 +141,20 @@ public static function defaults(): array
137141
/**
138142
* The field label can be set as string or associative array with translations
139143
*/
140-
'label' => function ($label = null) {
141-
return I18n::translate($label, $label);
144+
'label' => function (array|string|null $label = null) {
145+
return $this->i18n($label);
142146
},
143147
/**
144148
* Optional placeholder value that will be shown when the field is empty
145149
*/
146-
'placeholder' => function ($placeholder = null) {
147-
return I18n::translate($placeholder, $placeholder);
150+
'placeholder' => function (array|string|null $placeholder = null) {
151+
return $this->i18n($placeholder);
148152
},
149153
/**
150154
* If `true`, the field has to be filled in correctly to be saved.
151155
*/
152-
'required' => function (bool|null $required = null): bool {
153-
return $required ?? false;
156+
'required' => function (bool $required = false): bool {
157+
return $required;
154158
},
155159
/**
156160
* If `false`, the field will be disabled in non-default languages and cannot be translated. This is only relevant in multi-language setups.
@@ -161,62 +165,18 @@ public static function defaults(): array
161165
/**
162166
* Conditions when the field will be shown (since 3.1.0)
163167
*/
164-
'when' => function ($when = null) {
168+
'when' => function (array|null $when = null) {
165169
return $when;
166170
},
167171
/**
168172
* The width of the field in the field grid. Available widths: `1/1`, `1/2`, `1/3`, `1/4`, `2/3`, `3/4`
169173
*/
170-
'width' => function (string $width = '1/1') {
174+
'width' => function (string|null $width = null) {
171175
return $width;
172176
},
173177
'value' => function ($value = null) {
174178
return $value;
175179
}
176-
],
177-
'computed' => [
178-
'after' => function () {
179-
/** @var \Kirby\Form\Field $this */
180-
if ($this->after !== null) {
181-
return $this->model()->toString($this->after);
182-
}
183-
},
184-
'before' => function () {
185-
/** @var \Kirby\Form\Field $this */
186-
if ($this->before !== null) {
187-
return $this->model()->toString($this->before);
188-
}
189-
},
190-
'default' => function () {
191-
/** @var \Kirby\Form\Field $this */
192-
if ($this->default === null) {
193-
return;
194-
}
195-
196-
if (is_string($this->default) === false) {
197-
return $this->default;
198-
}
199-
200-
return $this->model()->toString($this->default);
201-
},
202-
'help' => function () {
203-
/** @var \Kirby\Form\Field $this */
204-
if ($this->help) {
205-
$help = $this->model()->toSafeString($this->help);
206-
$help = $this->kirby()->kirbytext($help);
207-
return $help;
208-
}
209-
},
210-
'label' => function () {
211-
/** @var \Kirby\Form\Field $this */
212-
return $this->model()->toString($this->label ?? Str::ucfirst($this->name));
213-
},
214-
'placeholder' => function () {
215-
/** @var \Kirby\Form\Field $this */
216-
if ($this->placeholder !== null) {
217-
return $this->model()->toString($this->placeholder);
218-
}
219-
}
220180
]
221181
];
222182
}
@@ -281,30 +241,14 @@ public function fill(mixed $value): static
281241
$this->applyProp('value', $this->options['props']['value'] ?? $value);
282242

283243
// reevaluate the computed props
284-
$this->applyComputed($this->options['computed']);
244+
$this->applyComputed($this->options['computed'] ?? []);
285245

286246
// reset the errors cache
287247
$this->errors = null;
288248

289249
return $this;
290250
}
291251

292-
/**
293-
* @deprecated 5.0.0 Use `::siblings() instead
294-
*/
295-
public function formFields(): Fields
296-
{
297-
return $this->siblings;
298-
}
299-
300-
/**
301-
* Checks if the field is disabled
302-
*/
303-
public function isDisabled(): bool
304-
{
305-
return $this->disabled === true;
306-
}
307-
308252
/**
309253
* Checks if the field is hidden
310254
*/
@@ -313,14 +257,6 @@ public function isHidden(): bool
313257
return ($this->options['hidden'] ?? false) === true;
314258
}
315259

316-
/**
317-
* Checks if the field is required
318-
*/
319-
public function isRequired(): bool
320-
{
321-
return $this->required ?? false;
322-
}
323-
324260
/**
325261
* Checks if the field is saveable
326262
*/
@@ -329,46 +265,6 @@ public function isSaveable(): bool
329265
return ($this->options['save'] ?? true) !== false;
330266
}
331267

332-
/**
333-
* Returns field api routes
334-
*/
335-
public function routes(): array
336-
{
337-
if (
338-
isset($this->options['api']) === true &&
339-
$this->options['api'] instanceof Closure
340-
) {
341-
return $this->options['api']->call($this);
342-
}
343-
344-
return [];
345-
}
346-
347-
/**
348-
* Checks if the field is saveable
349-
* @deprecated 5.0.0 Use `::isSaveable()` instead
350-
*/
351-
public function save(): bool
352-
{
353-
return $this->isSaveable();
354-
}
355-
356-
/**
357-
* Parent collection with all fields of the current form
358-
*/
359-
public function siblings(): Fields
360-
{
361-
return $this->siblings;
362-
}
363-
364-
/**
365-
* Returns all sibling fields for the HasSiblings trait
366-
*/
367-
protected function siblingsCollection(): Fields
368-
{
369-
return $this->siblings;
370-
}
371-
372268
/**
373269
* Converts the field to a plain array
374270
*/

0 commit comments

Comments
 (0)