Skip to content

Commit

Permalink
[FIX] UI: properly count inputs of Field\HasDynamicInputs templates. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
thibsy authored Nov 25, 2024
1 parent 00a1cb6 commit f48da0c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
/**
* This file is part of ILIAS, a powerful learning management system
* published by ILIAS open source e-Learning e.V.
*
* ILIAS is licensed with the GPL-3.0,
* see https://www.gnu.org/licenses/gpl-3.0.en.html
* You should have received a copy of said license along with the
* source code, too.
*
* If this is not the case or you just want to try ILIAS, you'll find
* us at:
* https://www.ilias.de
* https://github.com/ILIAS-eLearning
*
* *******************************************************************
*
* this script is responsible for clientside rendering of Inputs
* ILIAS\UI\Component\Input\Field\DynamicInputsAware.
*
Expand Down Expand Up @@ -82,13 +97,9 @@ il.UI.Input = il.UI.Input || {};
* @param {int} sub_input_count
*/
let addInputTemplateIds = function (template_html, sub_input_count) {
if (1 >= sub_input_count) {
return replaceAll(template_html, INPUT_ID_PLACEHOLDER, generateId());
}

// Ids must not be all the same, therefore we need to generate
// one for each sub-input contained in the template.
for (let i = 0; i < sub_input_count; i++) {
for (let i = 0; i <= sub_input_count; i++) {
template_html = replaceAll(
template_html,
`${INPUT_ID_PLACEHOLDER}_${i}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ protected function initClientsideRenderer(
string $template_html
): FI\HasDynamicInputs {
$dynamic_inputs_template_html = $this->replaceTemplateIds($template_html);
$dynamic_input_count = count($input->getDynamicInputs());
$dynamic_input_count = $this->countInputsWithoutGroupsRecursively($input->getTemplateForDynamicInputs());

// note that $dynamic_inputs_template_html is in tilted single quotes (`),
// because otherwise the html syntax might collide with normal ones.
Expand All @@ -980,6 +980,23 @@ protected function initClientsideRenderer(
});
}

/**
* Counts all inputs and nested inputs of groups, without counting groups themselves.
*/
protected function countInputsWithoutGroupsRecursively(FormInput|Input $input): int
{
if (!($input instanceof Component\Input\Group)) {
return 1;
}

$count = 0;
foreach ($input->getInputs() as $sub_input) {
$count += $this->countInputsWithoutGroupsRecursively($sub_input);
}

return $count;
}

protected function replaceTemplateIds(string $template_html): string
{
// regex matches anything between 'id="' and '"', hence the js_id.
Expand Down

0 comments on commit f48da0c

Please sign in to comment.