Skip to content

Commit

Permalink
[BUGFIX] Support associative "items" of check/radio fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mbrodala committed May 21, 2024
1 parent 444ba8f commit f8e6e52
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
9 changes: 6 additions & 3 deletions Classes/DataCollector/TCA/PlainValueProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function processCheckboxField($value, $fieldTca)
$activeItemKeys = BinaryConversionUtility::convertCheckboxValue($value, $itemCount);

foreach ($activeItemKeys as $key) {
$label = $fieldTca['items'][$key][0];
$label = $fieldTca['items'][$key]['label'] ?? $fieldTca['items'][$key][0];

if (str_starts_with((string) $label, 'LLL:')) {
$label = $this->getLanguageService()->sL($label);
Expand All @@ -64,8 +64,11 @@ public function processRadioField($value, $fieldTca)

if (is_array($fieldTca['items'])) {
foreach ($fieldTca['items'] as $set) {
if ((string)$set[1] === (string)$value) {
$label = $set[0];
$setLabel = $set['label'] ?? $set[0];
$setValue = $set['value'] ?? $set[1];

if ((string)$setValue === (string)$value) {
$label = $setLabel;
break;
}
}
Expand Down
47 changes: 47 additions & 0 deletions Tests/Unit/DataCollector/TCA/PlainValueProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,30 @@ public function convertsCheckboxValues()
$this->assertEquals($expectedOutput, $output);
}

/**
* @test
*/
public function supportsAssociativeCheckboxItems()
{
$fieldTca = [
'type' => 'check',
'items' => [
['label' => 'foo', 'value' => ''],
['label' => 'baz', 'value' => ''],
['label' => 'foobar', 'value' => ''],
['label' => 'foobarbaz', 'value' => ''],
],
];

$value = 3;

$expectedOutput = 'foo, baz';

$output = $this->plainValueProcessor->processCheckboxField($value, $fieldTca);

$this->assertEquals($expectedOutput, $output);
}

/**
* @test
*/
Expand All @@ -75,6 +99,29 @@ public function convertsRadioValues()
$this->assertEquals($expectedOutput, $output);
}

/**
* @test
*/
public function supportsAssociativeRadioItems()
{
$fieldTca = [
'type' => 'radio',
'items' => [
['label' => 'foo', 'value' => 1],
['label' => 'baz', 'value' => 2],
['label' => 'foobar', 'value' => 3],
],
];

$value = 2;

$expectedOutput = "baz";

$output = $this->plainValueProcessor->processRadioField($value, $fieldTca);

$this->assertEquals($expectedOutput, $output);
}

/**
* @test
*/
Expand Down

0 comments on commit f8e6e52

Please sign in to comment.