Skip to content

Commit 474ba15

Browse files
[6.x] Fallback to option key in listings when label is missing (#14429)
1 parent 7bc8816 commit 474ba15

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

src/Fieldtypes/HasSelectOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private function getLabel($actualValue)
138138

139139
$option = collect($this->getOptions())->filter(fn ($option) => $option['value'] === $value)->first();
140140

141-
return $option ? $option['label'] : $actualValue;
141+
return $option ? ($option['label'] ?? $option['value']) : $actualValue;
142142
}
143143

144144
private function castToBoolean($value)

tests/Fieldtypes/HasSelectOptionsTests.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,52 @@ public static function optionsProvider()
5858
],
5959
];
6060
}
61+
62+
#[Test]
63+
#[DataProvider('preProcessIndexProvider')]
64+
public function it_preprocesses_index_values($options, $value, $expected)
65+
{
66+
$field = $this->field(['options' => $options]);
67+
68+
$this->assertSame($expected, $field->preProcessIndex($value));
69+
}
70+
71+
public static function preProcessIndexProvider()
72+
{
73+
return [
74+
'list' => [
75+
['one', 'two', 'three'],
76+
'two',
77+
['two'],
78+
],
79+
'associative with labels' => [
80+
['one' => 'One', 'two' => 'Two', 'three' => 'Three'],
81+
'two',
82+
['Two'],
83+
],
84+
'associative without labels' => [
85+
['one' => null, 'two' => null, 'three' => null],
86+
'two',
87+
['two'],
88+
],
89+
'multidimensional with labels' => [
90+
[
91+
['key' => 'one', 'value' => 'One'],
92+
['key' => 'two', 'value' => 'Two'],
93+
['key' => 'three', 'value' => 'Three'],
94+
],
95+
'two',
96+
['Two'],
97+
],
98+
'multidimensional without labels' => [
99+
[
100+
['key' => 'one', 'value' => null],
101+
['key' => 'two', 'value' => null],
102+
['key' => 'three', 'value' => null],
103+
],
104+
'two',
105+
['two'],
106+
],
107+
];
108+
}
61109
}

0 commit comments

Comments
 (0)