Skip to content
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

Fix rendering of user-entered values in editable listbox-multiple TVs #16242

Merged
merged 2 commits into from
Nov 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand All @@ -14,43 +15,69 @@
* @package modx
* @subpackage processors.element.tv.renders.mgr.input
*/
class modTemplateVarInputRenderListboxMultiple extends modTemplateVarInputRender {
public function getTemplate() {
class modTemplateVarInputRenderListboxMultiple extends modTemplateVarInputRender
{
public function getTemplate()
{
return 'element/tv/renders/input/listbox-multiple.tpl';
}
public function process($value,array $params = []) {
$value = explode("||",$value);

public function process($value, array $params = [])
{
$savedValues = explode('||', $value);
$options = $this->getInputOptions();

$items = [];
$selections = [];
$optsValues = [];

foreach ($options as $option) {
$opt = explode("==",$option);
if (!isset($opt[1])) $opt[1] = $opt[0];
$items[] = [
'text' => htmlspecialchars($opt[0],ENT_COMPAT,'UTF-8'),
'value' => htmlspecialchars($opt[1],ENT_COMPAT,'UTF-8'),
'selected' => in_array($opt[1],$value),
];
$opt = explode('==', $option);
if (!isset($opt[1])) {
$opt[1] = $opt[0];
}
$optLabel = htmlspecialchars($opt[0], ENT_COMPAT, 'UTF-8');
$optValue = htmlspecialchars($opt[1], ENT_COMPAT, 'UTF-8');

/*
Collect defined options values for later comparison to savedValues
to determine if any custom user-entered values need to be accounted for.
*/
$optsValues[] = $optValue;

if (in_array($opt[1], $savedValues)) {
$selections[] = [
'text' => $optLabel,
'value' => $optValue,
'selected' => 1
];
} else {
$items[] = [
'text' => $optLabel,
'value' => $optValue,
'selected' => 0
];
}
}

// preserve the order of selected values
$orderedItems = [];
// loop trough the selected values
foreach ($value as $val) {
// find the corresponding option in the items array
foreach ($items as $item => $values) {
// if found, add it in the right order to the $orderItems array
if ($values['value'] == $val) {
$orderedItems[] = $values;
// and remove it from the original $items array
unset($items[$item]);
// Ensure custom values are displayed when the listbox is editable
if (isset($params['forceSelection']) && empty($params['forceSelection'])) {
$customValues = array_diff($savedValues, $optsValues);
if (!empty($customValues)) {
$customData = [];
foreach ($customValues as $customValue) {
$customValue = htmlspecialchars($customValue, ENT_COMPAT, 'UTF-8');
$customData[] = [
'text' => $customValue,
'value' => $customValue,
'selected' => 1
];
}
$selections = array_merge($selections, $customData);
}
}
// merge the correctly ordered items with the unselected remaining ones
$items = array_merge($orderedItems, $items);
$items = array_merge($selections, $items);

$this->setPlaceholder('opts',$items);
$this->setPlaceholder('opts', $items);
}
}
return 'modTemplateVarInputRenderListboxMultiple';