MDL-71683 navigation: Add a lang submenu within the user menu

- Part of: MDL-69588
The menu used to select one of the available languages is now
displayed as a submenu within the user menu only when a user is
logged in.
This commit is contained in:
Mihail Geshoski
2021-08-23 17:46:40 +08:00
committed by Mathew May
parent 7318b68b77
commit d78dc95d6b
4 changed files with 48 additions and 5 deletions
+43 -4
View File
@@ -58,8 +58,8 @@ class primary implements renderable, templatable {
return [
'primary' => $this->get_primary_nav(),
'custom' => $this->get_custom_menu($output),
'lang' => $this->get_lang_menu($output),
'user' => $this->get_user_menu(),
'lang' => !isloggedin() || isguestuser() ? $this->get_lang_menu($output) : [],
'user' => $this->get_user_menu($output),
];
}
@@ -131,6 +131,7 @@ class primary implements renderable, templatable {
$node = [
'title' => $langname,
'text' => $langname,
'link' => true,
'isactive' => $isactive,
'url' => $isactive ? new \moodle_url('#') : new \moodle_url($this->page->url, ['lang' => $langtype]),
];
@@ -143,15 +144,18 @@ class primary implements renderable, templatable {
/**
* Get/Generate the user menu.
*
* This is leveraging the data from user_get_user_navigation_info and the logic in $OUTPUT->user_menu()
*
* @param renderer_base $output
* @return array
*/
public function get_user_menu(): array {
public function get_user_menu(renderer_base $output): array {
global $CFG, $USER, $PAGE;
require_once($CFG->dirroot . '/user/lib.php');
$usermenudata = [];
$submenusdata = [];
$info = user_get_user_navigation_info($USER, $PAGE);
if (isset($info->unauthenticateduser)) {
$info->unauthenticateduser['content'] = get_string($info->unauthenticateduser['content']);
@@ -214,10 +218,45 @@ class primary implements renderable, templatable {
return $value;
}, $info->navitems);
// Include the language menu as a submenu within the user menu.
$langmenu = $this->get_lang_menu($output);
if (!empty($langmenu)) {
$languageitems = $langmenu['items'];
// If there are available languages, generate the data for the the language selector submenu.
if (!empty($languageitems)) {
$langsubmenuid = uniqid();
// Generate the data for the link to language selector submenu.
$language = (object) [
'itemtype' => 'submenu-link',
'submenuid' => $langsubmenuid,
'title' => get_string('language'),
'pixicon' => 'i/language',
'divider' => false,
'submenulink' => true,
];
// Place the link before the 'Log out' menu item which is either the last item in the menu or
// second to last when 'Switch roles' is available.
$menuposition = count($modifiedarray) - 1;
if (has_capability('moodle/role:switchroles', $PAGE->context)) {
$menuposition = count($modifiedarray) - 2;
}
array_splice($modifiedarray, $menuposition, 0, [$language]);
// Generate the data for the language selector submenu.
$submenusdata[] = (object)[
'id' => $langsubmenuid,
'title' => get_string('languageselector'),
'items' => $languageitems,
];
}
}
// Add dividers after the first item and before the last item.
$modifiedarray[0]->divider = true;
$modifiedarray[count($info->navitems) - 2]->divider = true;
$modifiedarray[count($modifiedarray) - 2]->divider = true;
$usermenudata['items'] = $modifiedarray;
$usermenudata['submenus'] = array_values($submenusdata);
return $usermenudata;
}
@@ -345,6 +345,7 @@ class icon_system_fontawesome extends icon_system_font {
'core:i/warning' => 'fa-exclamation text-warning',
'core:i/window_close' => 'fa-window-close',
'core:i/withsubcat' => 'fa-plus-square',
'core:i/language' => 'fa-language',
'core:m/USD' => 'fa-usd',
'core:t/addcontact' => 'fa-address-card',
'core:t/add' => 'fa-plus',
+1
View File
@@ -65,6 +65,7 @@ $templatecontext = [
'hasregionmainsettingsmenu' => !empty($regionmainsettingsmenu),
'primarymoremenu' => $OUTPUT->more_menu(array_merge($primarymenu['primary'], $primarymenu['custom']), 'navbar-nav'),
'secondarymoremenu' => $secondarynavigation,
'usermenu' => $primarymenu['user'],
];
$nav = $PAGE->flatnav;
$templatecontext['flatnavigation'] = $nav;
+3 -1
View File
@@ -65,7 +65,9 @@
{{{ output.search_box }}}
{{{ output.navbar_plugin_output }}}
<div class="d-flex align-items-stretch usermenu-container" data-region="usermenu">
{{{ output.user_menu }}}
{{#usermenu}}
{{> core/user_menu }}
{{/usermenu}}
</div>
</div>
</nav>