New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MODX 3.0.4-pl - Listbox Multiselect #16495
Comments
In the MODx 2.x branch, there is this code in the listbox-multiple render to "preserve the order of selected values". revolution/core/model/modx/processors/element/tv/renders/mgr/input/listbox-multiple.class.php Lines 34 to 47 in 3f887dc
In MODX 3, this code has been deleted in the PR #16242. Now when the resource is loaded, the selected values are always ordered in the same order as the Dropdown List Options in the TV. |
Would that feature of the multiselect be added back in? We tend to use the order the customer is choosing to show the resources that way. The first time it is saved it is in the customers choice order. Then the customer edits the page and not reorder the select and then it becomes the default order. |
I suppose so. It seems to be an oversight, not a conscious decision to remove it. To (temporarily) fix the issue, you could add this new code // preserve the order of selected values
$orderedSelections = [];
// loop trough the selected values
foreach ($savedValues as $val) {
// find the corresponding option in the selections array
foreach ($selections as $item => $values) {
// if found, add it in the right order to the $orderedSelections array
if ($values['value'] == $val) {
$orderedSelections[] = $values;
// and remove it from the original $selections array
unset($selections[$item]);
}
}
}
// merge the correctly ordered items with the remaining ones
$selections = array_merge($orderedSelections, $selections); before this line in the existing code: Line 78 in 73bfd27
@smg6511 Can you maybe take a look at this, as you were the one who deleted the code? |
A more elegant solution is probably to use a sort function: $savedValuesIndexLookup = array_flip($savedValues);
usort($selections, function($a, $b) use ($savedValuesIndexLookup) {
return $savedValuesIndexLookup[$a['value']] <=> $savedValuesIndexLookup[$b['value']];
}); |
@halftrainedharry @thorjoel - Yes, I'll take a look soon. I won't be able to focus on this until the beginning of December. I did remove that code on purpose, but it's been quite a while since that change was made so I want to go over it carefully before re-introducing the sorting. I'd also look to make it a config option in the TV creation panel, rather than a default behavior. |
Bug report
Summary
The order that the elements is chosen is saved, but when you refresh backend and look at the resource with the Template Variable the order is not shown.
Step to reproduce
Create a template variable with atleast two items. Use that template variable on a resource. Choose another order than the elements is showing in the list. Save. Refresh resource. Check template variable.
Observed behavior
The initial order is shown in the list, not the selected order.
Expected behavior
The selected order should be the order it is showing after a refresh. If the page is saved again, the initial order is saved and the elements are saved in "wrong" order.
Environment
MODX version 3.0.4-pl
The text was updated successfully, but these errors were encountered: