diff --git a/components/ILIAS/UI/src/Implementation/Component/Input/ViewControl/Renderer.php b/components/ILIAS/UI/src/Implementation/Component/Input/ViewControl/Renderer.php index dc702a5e3f0d..2242d777fdfe 100755 --- a/components/ILIAS/UI/src/Implementation/Component/Input/ViewControl/Renderer.php +++ b/components/ILIAS/UI/src/Implementation/Component/Input/ViewControl/Renderer.php @@ -22,6 +22,7 @@ use ILIAS\UI\Implementation\Render\AbstractComponentRenderer; use ILIAS\UI\Implementation\Render\ResourceRegistry; +use ILIAS\UI\Implementation\Render\Template; use ILIAS\UI\Renderer as RendererInterface; use ILIAS\UI\Component; use LogicException; @@ -132,14 +133,17 @@ protected function renderSortation(Sortation $component, RendererInterface $defa $internal_signal->addOption('value', $opt_value); $item = $ui_factory->button()->shy((string) $opt_label, '#') ->withOnClick($internal_signal); - $tpl->setCurrentBlock("option"); - $tpl->setVariable("OPTION", $default_renderer->render($item)); - - if ($opt_value === $component->getValue()) { - $tpl->touchBlock("selected"); - $tpl->setCurrentBlock("option"); - } - $tpl->parseCurrentBlock(); + $this->appendDropdownMenuItem( + $tpl, + 'option', + 'OPTION', + $default_renderer->render($item), + $this->isDropdownOptionSelected( + $opt_value, + $component->getValue(), + $opt_label === array_key_first($component->getOptions()) + ) + ); } if ($container_submit_signal = $component->getOnChangeSignal()) { @@ -306,13 +310,13 @@ protected function renderPagination(Pagination $component, RendererInterface $de $item = $ui_factory->button()->shy($option_label, '#') ->withOnClick($signal); - $tpl->setCurrentBlock("option_limit"); - $tpl->setVariable("OPTION_LIMIT", $default_renderer->render($item)); - if ($option === $limit) { - $tpl->touchBlock("selected"); - $tpl->setCurrentBlock("option_limit"); - } - $tpl->parseCurrentBlock(); + $this->appendDropdownMenuItem( + $tpl, + 'option_limit', + 'OPTION_LIMIT', + $default_renderer->render($item), + $option === $limit + ); } if ($container_submit_signal = $component->getOnChangeSignal()) { @@ -384,6 +388,51 @@ protected function renderMode(Mode $component, RendererInterface $default_render return $tpl->get(); } + protected function appendDropdownMenuItem( + Template $tpl, + string $block, + string $content_variable, + string $content, + bool $is_selected + ): void { + $tpl->setCurrentBlock($block); + $tpl->setVariable($content_variable, $content); + if ($is_selected) { + $tpl->touchBlock('selected'); + $tpl->setCurrentBlock($block); + } + $tpl->parseCurrentBlock(); + } + + /** + * @param array|null $current_value + */ + protected function isDropdownOptionSelected( + mixed $option_value, + ?array $current_value, + bool $is_sortation_default_option + ): bool { + if ($this->isUnsetViewControlValue($current_value)) { + return $is_sortation_default_option; + } + + return $option_value === $current_value; + } + + /** + * @param array|null $value + */ + protected function isUnsetViewControlValue(?array $value): bool + { + if ($value === null) { + return true; + } + + $first = $value[0] ?? null; + + return $first === null || $first === ''; + } + /** * @inheritdoc */ diff --git a/components/ILIAS/UI/tests/Component/Input/ViewControl/ViewControlSortationTest.php b/components/ILIAS/UI/tests/Component/Input/ViewControl/ViewControlSortationTest.php index 7499c2b089ef..b85005c78e94 100755 --- a/components/ILIAS/UI/tests/Component/Input/ViewControl/ViewControlSortationTest.php +++ b/components/ILIAS/UI/tests/Component/Input/ViewControl/ViewControlSortationTest.php @@ -93,7 +93,7 @@ public function testViewControlFieldSortationRendering(): void