Skip to content

Commit 47b47ef

Browse files
committed
fix(dropdownfields): handle empty value for entities dropdown
Signed-off-by: Thierry Bugier <[email protected]>
1 parent 2d726c9 commit 47b47ef

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

ajax/dropdown_values.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,28 @@
3737
|| $_REQUEST['dropdown_itemtype'] == '0'
3838
|| !class_exists($_REQUEST['dropdown_itemtype'])) {
3939
Dropdown::showFromArray(
40-
'dropdown_default_value',
40+
'dropdown_default_value',
4141
[], [
4242
'display_emptychoice' => true
4343
]
4444
);
4545
} else {
46+
$itemtype = $_REQUEST['dropdown_itemtype'];
4647
$question = new PluginFormcreatorQuestion();
4748
$question->getFromDB((int) $_REQUEST['id']);
48-
$defaultValue = isset($question->fields['default_values'])
49+
$defaultValue = isset($question->fields['default_values'])
4950
? $question->fields['default_values']
5051
: 0;
51-
Dropdown::show($_REQUEST['dropdown_itemtype'], [
52+
53+
$options = [
5254
'name' => 'dropdown_default_value',
5355
'rand' => mt_rand(),
5456
'value' => $defaultValue,
55-
]);
57+
];
58+
if ($itemtype == Entity::class) {
59+
$options['toadd'] = [
60+
-1 => Dropdown::EMPTY_VALUE,
61+
];
62+
}
63+
Dropdown::show($itemtype, $options);
5664
}

inc/fields/dropdownfield.class.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,16 @@ public function displayField($canEdit = true) {
253253

254254
$dparams['condition'] = $dparams_cond_crit;
255255

256-
$dparams['display_emptychoice'] = ($this->question->fields['show_empty'] !== '0');
256+
$dparams['display_emptychoice'] = false;
257+
if ($itemtype != Entity::class) {
258+
$dparams['display_emptychoice'] = ($this->question->fields['show_empty'] !== '0');
259+
} else {
260+
if ($this->question->fields['show_empty'] !== '0') {
261+
$dparams['toadd'] = [
262+
-1 => Dropdown::EMPTY_VALUE,
263+
];
264+
}
265+
}
257266

258267
$emptyItem = new $itemtype();
259268
$emptyItem->getEmpty();

0 commit comments

Comments
 (0)