Skip to content

Commit cfb345c

Browse files
authored
Merge pull request #1658 from craftcms/feature/1606-custom-options-support-radio-checkbox
adds support for importing custom options to radio and checkboxes field types
2 parents f8fd2eb + 82fb0d5 commit cfb345c

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/fields/Checkboxes.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,32 @@ public function parseField(): mixed
5555
$preppedData = [];
5656

5757
$options = Hash::get($this->field, 'settings.options');
58+
$customOptions = Hash::get($this->field, 'settings.customOptions', false);
5859
$match = Hash::get($this->fieldInfo, 'options.match', 'value');
5960

6061
foreach ($options as $option) {
61-
foreach ($value as $dataValue) {
62+
foreach ($value as $key => $dataValue) {
6263
if ($dataValue === $option[$match]) {
6364
$preppedData[] = $option['value'];
65+
unset($value[$key]);
6466
}
6567

6668
// special case for when mapping by label, but also using a default value
6769
// which relies on $option['value']
6870
if (empty($dataValue) && in_array($option['value'], $default)) {
6971
$preppedData[] = $option['value'];
72+
unset($value[$key]);
7073
}
7174
}
7275
}
7376

77+
// if custom options are allowed, and we still have values left in the $value variable - process those too
78+
if ($customOptions && !empty($value)) {
79+
foreach ($value as $dataValue) {
80+
$preppedData[] = $dataValue;
81+
}
82+
}
83+
7484
return $preppedData;
7585
}
7686
}

src/fields/RadioButtons.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function parseField(): mixed
5151
$default = Hash::get($this->fieldInfo, 'default');
5252

5353
$options = Hash::get($this->field, 'settings.options');
54+
$customOptions = Hash::get($this->field, 'settings.customOptions', false);
5455
$match = Hash::get($this->fieldInfo, 'options.match', 'value');
5556

5657
foreach ($options as $option) {
@@ -62,7 +63,8 @@ public function parseField(): mixed
6263
}
6364
}
6465

65-
if (empty($value)) {
66+
// if value is empty or the field supports adding custom options - return the value
67+
if (empty($value) || $customOptions) {
6668
return $value;
6769
}
6870

0 commit comments

Comments
 (0)