Skip to content

Commit ece77b9

Browse files
committed
Add abstract implementation for select options
1 parent 3508aac commit ece77b9

4 files changed

Lines changed: 63 additions & 38 deletions

File tree

wcfsetup/install/files/lib/system/form/option/CheckboxesFormOption.class.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use wcf\system\form\builder\field\MultipleSelectionFormField;
77
use wcf\system\form\option\formatter\IFormOptionFormatter;
88
use wcf\system\form\option\formatter\MultipleSelectionFormatter;
9-
use wcf\system\WCF;
10-
use wcf\util\JSON;
119

1210
/**
1311
* Implementation of a form field for selecting multiple values.
@@ -19,6 +17,8 @@
1917
*/
2018
class CheckboxesFormOption extends AbstractFormOption
2119
{
20+
use TSelectOptionsFormOption;
21+
2222
#[\Override]
2323
public function getId(): string
2424
{
@@ -29,23 +29,7 @@ public function getId(): string
2929
public function getFormField(string $id, array $configuration = []): AbstractFormField
3030
{
3131
$formField = MultipleSelectionFormField::create($id);
32-
33-
if (isset($configuration['selectOptions'])) {
34-
$selectOptions = [];
35-
foreach (JSON::decode($configuration['selectOptions']) as $selectOption) {
36-
if (isset($selectOption['value'][0])) {
37-
$value = $selectOption['value'][0];
38-
} else if (isset($selectOption['value'][WCF::getLanguage()->languageID])) {
39-
$value = $selectOption['value'][WCF::getLanguage()->languageID];
40-
} else {
41-
$value = reset($selectOption['value']);
42-
}
43-
44-
$selectOptions[$selectOption['key']] = $value;
45-
}
46-
47-
$formField->options($selectOptions);
48-
}
32+
$this->setSelectOptions($formField, $configuration);
4933

5034
return $formField;
5135
}

wcfsetup/install/files/lib/system/form/option/RadioButtonFormOption.class.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace wcf\system\form\option;
44

5+
use wcf\system\form\builder\field\AbstractFormField;
6+
use wcf\system\form\builder\field\RadioButtonFormField;
7+
58
/**
69
* Implementation of a form option for selecting a single value using radio buttons.
710
*
@@ -12,9 +15,20 @@
1215
*/
1316
class RadioButtonFormOption extends SelectFormOption
1417
{
18+
use TSelectOptionsFormOption;
19+
1520
#[\Override]
1621
public function getId(): string
1722
{
1823
return 'radioButton';
1924
}
25+
26+
#[\Override]
27+
public function getFormField(string $id, array $configuration = []): AbstractFormField
28+
{
29+
$formField = RadioButtonFormField::create($id);
30+
$this->setSelectOptions($formField, $configuration);
31+
32+
return $formField;
33+
}
2034
}

wcfsetup/install/files/lib/system/form/option/SelectFormOption.class.php

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
use wcf\system\form\builder\field\SelectFormField;
77
use wcf\system\form\option\formatter\IFormOptionFormatter;
88
use wcf\system\form\option\formatter\SelectFormatter;
9-
use wcf\system\WCF;
10-
use wcf\util\JSON;
119

1210
/**
1311
* Implementation of a form option for selecting a single value.
@@ -19,6 +17,8 @@
1917
*/
2018
class SelectFormOption extends AbstractFormOption
2119
{
20+
use TSelectOptionsFormOption;
21+
2222
#[\Override]
2323
public function getId(): string
2424
{
@@ -29,23 +29,7 @@ public function getId(): string
2929
public function getFormField(string $id, array $configuration = []): AbstractFormField
3030
{
3131
$formField = SelectFormField::create($id);
32-
33-
if (isset($configuration['selectOptions'])) {
34-
$selectOptions = [];
35-
foreach (JSON::decode($configuration['selectOptions']) as $selectOption) {
36-
if (isset($selectOption['value'][0])) {
37-
$value = $selectOption['value'][0];
38-
} else if (isset($selectOption['value'][WCF::getLanguage()->languageID])) {
39-
$value = $selectOption['value'][WCF::getLanguage()->languageID];
40-
} else {
41-
$value = reset($selectOption['value']);
42-
}
43-
44-
$selectOptions[$selectOption['key']] = $value;
45-
}
46-
47-
$formField->options($selectOptions);
48-
}
32+
$this->setSelectOptions($formField, $configuration);
4933

5034
return $formField;
5135
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace wcf\system\form\option;
4+
5+
use wcf\system\form\builder\field\ISelectionFormField;
6+
use wcf\system\WCF;
7+
use wcf\util\JSON;
8+
9+
/**
10+
* Provides helper methods for form options that use select options.
11+
*
12+
* @author Marcel Werk
13+
* @copyright 2001-2025 WoltLab GmbH
14+
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15+
* @since 6.2
16+
*/
17+
trait TSelectOptionsFormOption
18+
{
19+
/**
20+
* @param array<string, mixed> $configuration
21+
*/
22+
protected function setSelectOptions(ISelectionFormField $formField, array $configuration): void
23+
{
24+
if (!isset($configuration['selectOptions'])) {
25+
return;
26+
}
27+
28+
$selectOptions = [];
29+
foreach (JSON::decode($configuration['selectOptions']) as $selectOption) {
30+
if (isset($selectOption['value'][0])) {
31+
$value = $selectOption['value'][0];
32+
} else if (isset($selectOption['value'][WCF::getLanguage()->languageID])) {
33+
$value = $selectOption['value'][WCF::getLanguage()->languageID];
34+
} else {
35+
$value = reset($selectOption['value']);
36+
}
37+
38+
$selectOptions[$selectOption['key']] = $value;
39+
}
40+
41+
$formField->options($selectOptions);
42+
}
43+
}

0 commit comments

Comments
 (0)