Skip to content

Commit 626888f

Browse files
[6.x] Prevent browser re-sort of dictionary options with integer keys (#14875)
1 parent 3bd0eb5 commit 626888f

1 file changed

Lines changed: 8 additions & 2 deletions

File tree

src/Http/Controllers/CP/Fieldtypes/DictionaryFieldtypeController.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ class DictionaryFieldtypeController extends CpController
1111
{
1212
public function __invoke(Request $request, string $dictionary)
1313
{
14-
$fieldtype = $this->fieldtype($request);
14+
$options = $this->fieldtype($request)->dictionary()->options($request->search);
1515

16+
// Return an ordered list of key/value pairs rather than a value-keyed object.
17+
// When the values are integers, the browser would re-sort the object's keys ascending,
18+
// discarding the dictionary's own order.
1619
return [
17-
'data' => $fieldtype->dictionary()->options($request->search),
20+
'data' => collect($options)
21+
->map(fn ($label, $key) => ['key' => (string) $key, 'value' => $label])
22+
->values()
23+
->all(),
1824
];
1925
}
2026

0 commit comments

Comments
 (0)