Skip to content

Commit 3bbe568

Browse files
committed
Make the prefix form field a required field and change it to a generic IFormField.
1 parent 29ccdb1 commit 3bbe568

File tree

2 files changed

+45
-63
lines changed

2 files changed

+45
-63
lines changed

com.woltlab.wcf/templates/shared_prefixFormFieldContainer.tpl

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,59 @@
1-
<dl id="{$container->getField()->getPrefixedId()}Container"{*
2-
*}{if !$container->getField()->getClasses()|empty} class="{implode from=$container->getField()->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{*
3-
*}{foreach from=$container->getField()->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
4-
*}{if !$container->getField()->checkDependencies()} style="display: none
1+
{assign var='field' value=$container->getField()}
2+
{assign var='prefixField' value=$container->getPrefixField()}
3+
4+
<dl id="{$field->getPrefixedId()}Container"{*
5+
*}{if !$field->getClasses()|empty} class="{implode from=$field->getClasses() item='class' glue=' '}{$class}{/implode}"{/if}{*
6+
*}{foreach from=$field->getAttributes() key='attributeName' item='attributeValue'} {$attributeName}="{$attributeValue}"{/foreach}{*
7+
*}{if !$field->checkDependencies()} style="display: none
58
;"{/if}{*
69
*}>
7-
<dt>{if $container->getLabel() !== null}<label for="{$container->getField()->getPrefixedId()}">{unsafe:$container->getLabel()}</label>{if $container->getField()->isRequired() && $form->marksRequiredFields()} <span class="formFieldRequired">*</span>{/if}{/if}</dt>
10+
<dt>{if $container->getLabel() !== null}<label for="{$field->getPrefixedId()}">{unsafe:$container->getLabel()}</label>{if $field->isRequired() && $form->marksRequiredFields()} <span class="formFieldRequired">*</span>{/if}{/if}</dt>
811
<dd>
912
<div class="inputAddon">
10-
{if $container->getPrefixField() !== null && $container->getPrefixField()->isAvailable()}
13+
{if $prefixField->isAvailable()}
1114
{if !$container->prefixHasSelectableOptions()}
12-
{if $container->getPrefixLabel() !== ''}
13-
<span class="inputPrefix">{unsafe:$container->getPrefixLabel()}</span>
14-
{/if}
15+
<span class="inputPrefix">{unsafe:$prefixField->getFieldHtml()}</span>
1516
{else}
16-
<span class="inputPrefix dropdown" id="{$container->getPrefixField()->getPrefixedId()}_dropdown">
17+
<span class="inputPrefix dropdown" id="{$prefixField->getPrefixedId()}_dropdown">
1718
<span class="dropdownToggle">{unsafe:$container->getSelectedPrefixOption()[label]} {icon name='caret-down' type='solid'}</span>
1819

1920
<ul class="dropdownMenu">
20-
{foreach from=$container->getPrefixField()->getNestedOptions() item=__fieldNestedOption}
21-
<li{if ($container->getPrefixField()->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]) || !$__fieldNestedOption[isSelectable]} class="{if $container->getPrefixField()->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]}active{if !$__fieldNestedOption[isSelectable]} disabled{/if}{else}disabled{/if}"{/if} data-value="{$__fieldNestedOption[value]}" data-label="{$__fieldNestedOption[label]}"><span>{unsafe:'&nbsp;'|str_repeat:$__fieldNestedOption[depth] * 4}{unsafe:$__fieldNestedOption[label]}</span></li>
21+
{foreach from=$prefixField->getNestedOptions() item=__fieldNestedOption}
22+
<li{if ($prefixField->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]) || !$__fieldNestedOption[isSelectable]} class="{if $prefixField->getValue() == $__fieldNestedOption[value] && $__fieldNestedOption[isSelectable]}active{if !$__fieldNestedOption[isSelectable]} disabled{/if}{else}disabled{/if}"{/if} data-value="{$__fieldNestedOption[value]}" data-label="{$__fieldNestedOption[label]}"><span>{unsafe:'&nbsp;'|str_repeat:$__fieldNestedOption[depth] * 4}{unsafe:$__fieldNestedOption[label]}</span></li>
2223
{/foreach}
2324
</ul>
24-
<input type="hidden" id="{$container->getPrefixField()->getPrefixedId()}" name="{$container->getPrefixField()->getPrefixedId()}" value="{if $container->getPrefixField()->getValue() === null}{$container->getSelectedPrefixOption()[value]}{else}{$container->getPrefixField()->getValue()}{/if}" />
25+
<input type="hidden" id="{$prefixField->getPrefixedId()}" name="{$prefixField->getPrefixedId()}" value="{if $prefixField->getValue() === null}{$container->getSelectedPrefixOption()[value]}{else}{$prefixField->getValue()}{/if}" />
2526
</span>
26-
{include file='shared_formFieldDependencies' field=$container->getPrefixField()}
27-
{include file='shared_formFieldDataHandler' field=$container->getPrefixField()}
2827
{/if}
28+
{include file='shared_formFieldDependencies' field=$prefixField sandbox=true}
29+
{include file='shared_formFieldDataHandler' field=$prefixField sandbox=true}
2930
{/if}
30-
31-
{unsafe:$container->getField()->getFieldHtml()}
31+
{unsafe:$field->getFieldHtml()}
3232
</div>
3333

3434
{if $container->getDescription() !== null}
3535
<small>{unsafe:$container->getDescription()}</small>
3636
{/if}
3737

38-
{include file='shared_formFieldErrors' field=$container->getField()}
38+
{include file='shared_formFieldErrors' field=$field sandbox=true}
3939

40-
{if $container->getPrefixField() !== null && $container->getPrefixField()->isAvailable()}
41-
{foreach from=$container->getPrefixField()->getValidationErrors() item='validationError'}
40+
{if $prefixField !== null && $prefixField->isAvailable()}
41+
{foreach from=$prefixField->getValidationErrors() item='validationError'}
4242
{unsafe:$validationError->getHtml()}
4343
{/foreach}
4444
{/if}
4545

46-
{include file='shared_formFieldDependencies' field=$container->getField()}
47-
{include file='shared_formFieldDataHandler' field=$container->getField()}
46+
{include file='shared_formFieldDependencies' field=$field sandbox=true}
47+
{include file='shared_formFieldDataHandler' field=$field sandbox=true}
4848
</dd>
4949
</dl>
5050

51-
{if $container->getPrefixField() !== null && $container->getPrefixField()->isAvailable() && !$container->getPrefixField()->isImmutable() && $container->prefixHasSelectableOptions()}
51+
{if $prefixField->isAvailable() && !$prefixField->isImmutable() && $container->prefixHasSelectableOptions()}
5252
<script data-relocate="true">
5353
require(['WoltLabSuite/Core/Form/Builder/Container/SuffixFormField'], function(FormBuilderPrefixFormFieldContainer) {
5454
new FormBuilderPrefixFormFieldContainer(
5555
'{unsafe:$container->getDocument()->getId()|encodeJS}',
56-
'{unsafe:$container->getPrefixField()->getPrefixedId()|encodeJS}',
56+
'{unsafe:$prefixField->getPrefixedId()|encodeJS}',
5757
);
5858
});
5959
</script>

wcfsetup/install/files/lib/system/form/builder/container/PrefixConditionFormFieldContainer.class.php

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
final class PrefixConditionFormFieldContainer extends FormContainer
1919
{
2020
/**
21-
* form field to which the prefix selection is added
21+
* form field to which the prefix is added
2222
*/
2323
protected IFormField $field;
2424

2525
/**
26-
* selection form field containing the prefix options
26+
* form field containing the prefix field
2727
*/
28-
protected ?ISelectionFormField $prefixField;
28+
protected IFormField $prefixField;
2929

3030
/**
3131
* @inheritDoc
@@ -104,29 +104,28 @@ public function getField(): IFormField
104104
*/
105105
public function getSelectedPrefixOption(): array
106106
{
107-
if (!isset($this->prefixField)) {
108-
throw new \BadMethodCallException(
109-
"There is no prefix field for which a label could be determined for container '{$this->getId()}'."
110-
);
111-
}
112-
if (empty($this->getPrefixField()->getOptions())) {
107+
$prefixField = $this->getPrefixField();
108+
109+
\assert($prefixField instanceof ISelectionFormField);
110+
111+
if (empty($prefixField->getOptions())) {
113112
throw new \BadMethodCallException(
114113
"The prefix field has no options for container '{$this->getId()}'."
115114
);
116115
}
117116

118-
foreach ($this->getPrefixField()->getNestedOptions() as $option) {
119-
if ($this->getPrefixField()->getValue() === null) {
117+
foreach ($prefixField->getNestedOptions() as $option) {
118+
if ($prefixField->getValue() === null) {
120119
if ($option['isSelectable']) {
121120
return $option;
122121
}
123-
} elseif ($option['value'] == $this->getPrefixField()->getValue()) {
122+
} elseif ($option['value'] == $prefixField->getValue()) {
124123
return $option;
125124
}
126125
}
127126

128127
// Return the first selectable option if no valid value is selected.
129-
foreach ($this->getPrefixField()->getNestedOptions() as $option) {
128+
foreach ($prefixField->getNestedOptions() as $option) {
130129
if ($option['isSelectable']) {
131130
return $option;
132131
}
@@ -138,40 +137,23 @@ public function getSelectedPrefixOption(): array
138137
}
139138

140139
/**
141-
* Returns the selection form field containing the prefix options.
140+
* Returns the prefix form field.
142141
*/
143-
public function getPrefixField(): ?ISelectionFormField
142+
public function getPrefixField(): IFormField
144143
{
145-
return $this->prefixField;
146-
}
147-
148-
/**
149-
* Returns the label used for the prefix selection if the field has no selectable options
150-
* or is immutable.
151-
*/
152-
public function getPrefixLabel(): string
153-
{
154-
if ($this->getPrefixField() === null) {
144+
if (!isset($this->prefixField)) {
155145
throw new \BadMethodCallException(
156-
"There is no prefix field for which a label could be determined for container '{$this->getId()}'."
146+
"Prefix field has not been set yet for container '{$this->getId()}'."
157147
);
158148
}
159149

160-
if (empty($this->getPrefixField()->getOptions())) {
161-
return '';
162-
}
163-
164-
if (isset($this->getPrefixField()->getOptions()[$this->getPrefixField()->getValue()])) {
165-
return $this->getPrefixField()->getOptions()[$this->getPrefixField()->getValue()];
166-
}
167-
168-
return '';
150+
return $this->prefixField;
169151
}
170152

171153
/**
172-
* Sets the selection form field containing the prefix options.
154+
* Sets the prefix form field.
173155
*/
174-
public function prefixField(ISelectionFormField $formField): static
156+
public function prefixField(IFormField $formField): static
175157
{
176158
if (isset($this->prefixField)) {
177159
throw new \BadMethodCallException(
@@ -186,13 +168,13 @@ public function prefixField(ISelectionFormField $formField): static
186168
}
187169

188170
/**
189-
* Returns `true` if the prefix selection has any selectable options.
171+
* Returns `true` if the prefix form field has any selectable options.
190172
*/
191173
public function prefixHasSelectableOptions(): bool
192174
{
193175
$prefixField = $this->getPrefixField();
194176

195-
if ($prefixField === null) {
177+
if (!($prefixField instanceof ISelectionFormField)) {
196178
return false;
197179
}
198180

0 commit comments

Comments
 (0)