Skip to content

Commit 25e0dbf

Browse files
Merge pull request #7664 from getkirby/forms/cache-field-options
refact: Cache field options
2 parents 7ae686f + 4374cd7 commit 25e0dbf

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

config/fields/color.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
* to directly select them
5252
*/
5353
'options' => function (array $options = []): array {
54+
// make sure to flush the options cache when
55+
// new options are being passed
56+
$this->optionsCache = null;
5457
return $options;
5558
}
5659
],
@@ -59,6 +62,14 @@
5962
return Str::lower($this->default);
6063
},
6164
'options' => function (): array {
65+
return $this->optionsCache ??= $this->getOptions();
66+
}
67+
],
68+
'methods' => [
69+
'emptyValue' => function () {
70+
return '';
71+
},
72+
'getOptions' => function () {
6273
// resolve options to support manual arrays
6374
// alongside api and query options
6475
$props = FieldOptions::polyfill($this->props);
@@ -103,11 +114,6 @@
103114
}
104115

105116
return $options;
106-
}
107-
],
108-
'methods' => [
109-
'emptyValue' => function () {
110-
return '';
111117
},
112118
'isColor' => function (string $value): bool {
113119
return

config/fields/mixins/options.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
* An array with options
1515
*/
1616
'options' => function ($options = []) {
17+
// make sure to flush the options cache when
18+
// new options are being passed
19+
$this->optionsCache = null;
1720
return $options;
1821
},
1922
/**
@@ -25,7 +28,7 @@
2528
],
2629
'computed' => [
2730
'options' => function (): array {
28-
return $this->getOptions();
31+
return $this->optionsCache ??= $this->getOptions();
2932
}
3033
],
3134
'methods' => [

0 commit comments

Comments
 (0)