diff options
| author | Jan-Hendrik Willms <tleilax+studip@gmail.com> | 2023-03-11 15:27:13 +0000 |
|---|---|---|
| committer | David Siegfried <david.siegfried@uni-vechta.de> | 2023-03-11 15:27:13 +0000 |
| commit | 57d455a77c054be53b463a842d66fa8508d40c53 (patch) | |
| tree | 08ebe1b7df523f8cf50967ed383f773fbfcb0af4 /lib/classes/ActionMenu.php | |
| parent | 0da036073b530a8ca2316c0525d5a402ccd088b8 (diff) | |
allow forcing of rendering mode for ActionMenu (regardless of configured threshold) and add basic tests, fixes #2210
Closes #2210
Merge request studip/studip!1452
Diffstat (limited to 'lib/classes/ActionMenu.php')
| -rw-r--r-- | lib/classes/ActionMenu.php | 53 |
1 files changed, 50 insertions, 3 deletions
diff --git a/lib/classes/ActionMenu.php b/lib/classes/ActionMenu.php index f7ec888..6fc016a 100644 --- a/lib/classes/ActionMenu.php +++ b/lib/classes/ActionMenu.php @@ -11,6 +11,9 @@ class ActionMenu const TEMPLATE_FILE_SINGLE = 'shared/action-menu-single.php'; const TEMPLATE_FILE_MULTIPLE = 'shared/action-menu.php'; + const RENDERING_MODE_ICONS = 'icons'; + const RENDERING_MODE_MENU = 'menu'; + /** * Returns an instance. * @@ -32,6 +35,10 @@ class ActionMenu */ protected $context = ''; + /** + * @var string|null $rendering_mode The forced rendering mode + */ + protected $rendering_mode = null; /** * Private constructur. @@ -248,10 +255,10 @@ class ActionMenu return ''; } - $template_file = count($this->actions) <= Config::get()->ACTION_MENU_THRESHOLD + $template_file = $this->getRenderingMode() === self::RENDERING_MODE_ICONS ? self::TEMPLATE_FILE_SINGLE : self::TEMPLATE_FILE_MULTIPLE; - +; $template = $GLOBALS['template_factory']->open($template_file); $template->actions = array_map(function ($action) { $disabled = isset($action['attributes']['disabled']) @@ -287,7 +294,6 @@ class ActionMenu return null; } - /** * Sets the context for the menu. * @@ -302,6 +308,47 @@ class ActionMenu return $this; } + /** + * Forces an explicit rendering mode. + * + * @param string|null $mode The desired rendering mode or null for automatic detection + * @return ActionMenu The action menu instance to allow chaining + * @throws Exception + */ + public function setRenderingMode(?string $mode): ActionMenu + { + if ( + $mode !== null + && $mode !== self::RENDERING_MODE_ICONS + && $mode !== self::RENDERING_MODE_MENU + ) { + throw new Exception("Invalid rendering mode '{$mode}'"); + } + + $this->rendering_mode = $mode; + + return $this; + } + + /** + * Returns the rendering mode for this action menu. This is set by either + * calling setRenderingMode or automatically determined by the configured + * threshold. + * + * @return string + */ + public function getRenderingMode(): string + { + $rendering_mode = $this->rendering_mode; + + if ($rendering_mode === null) { + $rendering_mode = count($this->actions) <= Config::get()->ACTION_MENU_THRESHOLD + ? self::RENDERING_MODE_ICONS + : self::RENDERING_MODE_MENU; + } + + return $rendering_mode; + } /** * Generates the title of the action menu, including its context, if the context has been set. |
