File tree Expand file tree Collapse file tree
wcfsetup/install/files/lib/system/form/option Expand file tree Collapse file tree Original file line number Diff line number Diff line change 66use wcf \system \form \builder \field \MultipleSelectionFormField ;
77use wcf \system \form \option \formatter \IFormOptionFormatter ;
88use 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.
1917 */
2018class 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 }
Original file line number Diff line number Diff line change 22
33namespace 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 *
1215 */
1316class 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}
Original file line number Diff line number Diff line change 66use wcf \system \form \builder \field \SelectFormField ;
77use wcf \system \form \option \formatter \IFormOptionFormatter ;
88use 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.
1917 */
2018class 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 }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments