This repository has been archived by the owner on Feb 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
79 lines (75 loc) · 1.85 KB
/
index.php
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
<?php
use Kirby\Cms\App;
use Kirby\Cms\Content;
use Kirby\Form\Form;
use Kirby\Data\Yaml;
App::plugin('oblik/entity-field', [
'options' => [
'toggle' => false,
],
'fields' => [
'entity' => [
'props' => [
'fields' => function (array $fields) {
return $fields;
},
'icon' => function ($icon = null) {
return $icon;
},
'toggle' => function ($toggle = null) {
return $toggle ?? option('oblik.entity-field.toggle');
},
],
'computed' => [
'value' => function () {
$data = Yaml::decode($this->value);
return $this->form($data)->values();
},
'fields' => function () {
if (empty($this->fields)) {
throw new Exception('Please provide some fields for the entity');
}
return $this->form()->fields()->toArray();
}
],
'methods' => [
/**
* Similar to the `form()` method of the native structure field:
* https://github.com/getkirby/kirby/blob/3.7.3/config/fields/structure.php#L172-L178
*/
'form' => function (array $values = []) {
return new Form([
'fields' => $this->attrs['fields'],
'values' => $values,
'model' => $this->model
]);
},
],
'save' => function ($value) {
$data = $this->form($value)->data(true);
$dataFiltered = array_filter($data);
if (count($dataFiltered) > 0) {
return $dataFiltered;
} else {
return null;
}
},
'validations' => [
// needs custom validator to validate the form with:
// $this->field()->form($this->requestBody())->errors()
// before saving @see https://github.com/getkirby/ideas/issues/277
]
]
],
'fieldMethods' => [
'toEntity' => function ($field) {
return new Content($field->yaml(), $field->parent());
}
],
'translations' => [
'en' => [
'oblik.entity-field.field' => 'Field',
'oblik.entity-field.fields' => 'Fields'
],
]
]);