Skip to content

Commit 40cd2bc

Browse files
committed
refact!: Models fields use models picker dialogs
1 parent 166a400 commit 40cd2bc

34 files changed

+700
-971
lines changed

config/fields/files.php

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

33
use Kirby\Cms\ModelWithContent;
4+
use Kirby\Panel\Controller\Dialog\FilesPickerDialogController;
45

56
return [
67
'mixins' => [
7-
'filepicker',
88
'layout',
99
'min',
1010
'picker',
@@ -21,7 +21,8 @@
2121
'placeholder' => null,
2222

2323
/**
24-
* Sets the file(s), which are selected by default when a new page is created
24+
* Sets the file(s), which are selected by default
25+
* when a new page is created
2526
*/
2627
'default' => function ($default = null) {
2728
return $default;
@@ -48,9 +49,6 @@
4849
'parent' => function () {
4950
return $this->parentModel->apiUrl(true);
5051
},
51-
'query' => function () {
52-
return $this->query ?? $this->parentModel::CLASS_ALIAS . '.files';
53-
},
5452
'default' => function () {
5553
return $this->toFormValues($this->default);
5654
},
@@ -60,51 +58,48 @@
6058
],
6159
'methods' => [
6260
'toModel' => function (string $id) {
63-
return $this->kirby()->file($id, $this->model());
61+
return $this->kirby()->file($id, $this->model);
6462
}
6563
],
6664
'api' => function () {
6765
return [
6866
[
69-
'pattern' => '/',
70-
'action' => function () {
71-
$field = $this->field();
72-
73-
return $field->filepicker([
74-
'image' => $field->image(),
75-
'info' => $field->info(),
76-
'layout' => $field->layout(),
77-
'limit' => $field->limit(),
78-
'page' => $this->requestQuery('page'),
79-
'query' => $field->query(),
80-
'search' => $this->requestQuery('search'),
81-
'text' => $field->text()
82-
]);
83-
}
67+
'pattern' => 'items',
68+
'method' => 'GET',
69+
'action' => $this->field()->itemsFromRequest(...)
8470
],
8571
[
8672
'pattern' => 'upload',
8773
'method' => 'POST',
8874
'action' => function () {
89-
$field = $this->field();
90-
$uploads = $field->uploads();
75+
$field = $this->field();
9176

9277
// move_uploaded_file() not working with unit test
9378
// @codeCoverageIgnoreStart
94-
return $field->upload($this, $uploads, function ($file, $parent) use ($field) {
95-
return $file->panel()->pickerData([
96-
'image' => $field->image(),
97-
'info' => $field->info(),
98-
'layout' => $field->layout(),
99-
'model' => $field->model(),
100-
'text' => $field->text(),
101-
]);
102-
});
79+
return $field->upload(
80+
$this,
81+
$field->uploads(),
82+
fn ($file, $parent) => $field->toItem($file)
83+
);
10384
// @codeCoverageIgnoreEnd
10485
}
10586
]
10687
];
10788
},
89+
'dialogs' => fn () => [
90+
'picker' => fn () => new FilesPickerDialogController(...[
91+
'model' => $this->model(),
92+
'hasSearch' => $this->search,
93+
'image' => $this->image,
94+
'info' => $this->info ?? false,
95+
'limit' => $this->limit,
96+
'max' => $this->max,
97+
'multiple' => $this->multiple,
98+
'query' => $this->query,
99+
'text' => $this->text,
100+
...$this->picker
101+
])
102+
],
108103
'save' => function ($value = null) {
109104
return $this->toStoredValues($value);
110105
},

config/fields/mixins/filepicker.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

config/fields/mixins/pagepicker.php

Lines changed: 0 additions & 14 deletions
This file was deleted.

config/fields/mixins/picker.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@
5959
return $multiple;
6060
},
6161

62+
/**
63+
* Additional picker dialog props
64+
* @since 6.0.0
65+
* @values { image, layout, size }
66+
*/
67+
'picker' => function (array $picker = []) {
68+
return $picker;
69+
},
70+
6271
/**
6372
* Query for the items to be included in the picker
6473
*/
@@ -98,6 +107,12 @@
98107
'getIdFromArray' => function (array $array) {
99108
return $array['uuid'] ?? $array['id'] ?? null;
100109
},
110+
'itemsFromRequest' => function () {
111+
$ids = $this->kirby()->request()->get('items', '');
112+
$ids = Str::split($ids);
113+
$models = $this->toModels($ids);
114+
return $this->toItems($models);
115+
},
101116
'toItem' => function (ModelWithContent $model) {
102117
return $model->panel()->pickerData([
103118
'image' => $this->image,
@@ -107,7 +122,7 @@
107122
'text' => $this->text,
108123
]);
109124
},
110-
'toItems' => function (array $models) {
125+
'toItems' => function (array $models = []) {
111126
return A::map(
112127
$models,
113128
fn ($model) => $this->toItem($model)
@@ -116,25 +131,31 @@
116131
'toModel' => function (string $id) {
117132
throw new Exception(message: 'toModel() is not implemented on ' . $this->type() . ' field');
118133
},
134+
'toModels' => function (array $ids = []) {
135+
return A::map(
136+
$ids,
137+
fn ($id) => $this->toModel($id)
138+
);
139+
},
119140
'toFormValues' => function ($value = null) {
120-
$items = [];
141+
$ids = [];
121142

122143
foreach (Data::decode($value, 'yaml') as $id) {
123144
if (is_array($id) === true) {
124145
$id = $this->getIdFromArray($id);
125146
}
126147

127148
if ($id !== null && ($model = $this->toModel($id))) {
128-
$items[] = $this->toItem($model);
149+
$ids[] = $model->id();
129150
}
130151
}
131152

132-
return $items;
153+
return $ids;
133154
},
134155
'toStoredValues' => function ($value = null) {
135156
return A::map(
136157
$value ?? [],
137-
fn (array $item) => $item[$this->store]
158+
fn (string $id) => (string)$this->toModel($id)?->{$this->store}()
138159
);
139160
},
140161
]

config/fields/mixins/userpicker.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

config/fields/pages.php

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

3-
use Kirby\Toolkit\A;
3+
use Kirby\Panel\Controller\Dialog\PagesPickerDialogController;
44

55
return [
66
'mixins' => [
77
'layout',
88
'min',
9-
'pagepicker',
109
'picker',
1110
],
1211
'props' => [
@@ -58,26 +57,27 @@
5857
'api' => function () {
5958
return [
6059
[
61-
'pattern' => '/',
62-
'action' => function () {
63-
$field = $this->field();
64-
65-
return $field->pagepicker([
66-
'image' => $field->image(),
67-
'info' => $field->info(),
68-
'layout' => $field->layout(),
69-
'limit' => $field->limit(),
70-
'page' => $this->requestQuery('page'),
71-
'parent' => $this->requestQuery('parent'),
72-
'query' => $field->query(),
73-
'search' => $this->requestQuery('search'),
74-
'subpages' => $field->subpages(),
75-
'text' => $field->text()
76-
]);
77-
}
60+
'pattern' => 'items',
61+
'method' => 'GET',
62+
'action' => $this->field()->itemsFromRequest(...)
7863
]
7964
];
8065
},
66+
'dialogs' => fn () => [
67+
'picker' => fn () => new PagesPickerDialogController(...[
68+
'model' => $this->model(),
69+
'hasSearch' => $this->search,
70+
'image' => $this->image,
71+
'info' => $this->info ?? false,
72+
'limit' => $this->limit,
73+
'max' => $this->max,
74+
'multiple' => $this->multiple,
75+
'query' => $this->query,
76+
'subpages' => $this->subpages,
77+
'text' => $this->text,
78+
...$this->picker
79+
])
80+
],
8181
'save' => function ($value = null) {
8282
return $this->toStoredValues($value);
8383
},

config/fields/textarea.php

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

3+
use Kirby\Panel\Controller\Dialog\FilesPickerDialogController;
4+
35
return [
4-
'mixins' => ['filepicker', 'upload'],
6+
'mixins' => ['upload'],
57
'props' => [
68
/**
79
* Unset inherited props
@@ -86,33 +88,34 @@
8688
],
8789
'api' => function () {
8890
return [
89-
[
90-
'pattern' => 'files',
91-
'action' => function () {
92-
return $this->field()->filepicker([
93-
...$this->field()->files(),
94-
'page' => $this->requestQuery('page'),
95-
'search' => $this->requestQuery('search')
96-
]);
97-
}
98-
],
9991
[
10092
'pattern' => 'upload',
10193
'method' => 'POST',
10294
'action' => function () {
103-
$field = $this->field();
104-
$uploads = $field->uploads();
95+
$field = $this->field();
10596

106-
return $this->field()->upload($this, $uploads, fn ($file, $parent) => [
107-
'filename' => $file->filename(),
108-
'dragText' => $file->panel()->dragText(
109-
absolute: $field->model()->is($parent) === false
110-
),
111-
]);
97+
return $field->upload(
98+
$this,
99+
$field->uploads(),
100+
fn ($file, $parent) => [
101+
'filename' => $file->filename(),
102+
'dragText' => $file->panel()->dragText(
103+
absolute: $field->model()->is($parent) === false
104+
),
105+
]
106+
);
112107
}
113108
]
114109
];
115110
},
111+
'dialogs' => fn () => [
112+
'files' => fn () => new FilesPickerDialogController(...[
113+
'multiple' => false,
114+
...$this->files(),
115+
'model' => $this->model(),
116+
'uploads' => $this->uploads,
117+
])
118+
],
116119
'validations' => [
117120
'minlength',
118121
'maxlength'

0 commit comments

Comments
 (0)