MDL-72885 output: Add lang attribute for language menu items

This commit is contained in:
Jun Pataleta 2021-10-22 20:50:44 +08:00
parent 326d502c5c
commit 67da548b48
4 changed files with 35 additions and 6 deletions

View File

@ -96,6 +96,14 @@ class language_menu implements \renderable, \templatable {
// Add the lang picker if needed.
foreach ($this->langs as $langtype => $langname) {
$isactive = $langtype == $this->currentlang;
$attributes = [];
if (!$isactive) {
// Set the lang attribute for languages different from the page's current language.
$attributes[] = [
'key' => 'lang',
'value' => get_html_lang_attribute_value($langtype),
];
}
$node = [
'title' => $langname,
'text' => $langname,
@ -103,6 +111,9 @@ class language_menu implements \renderable, \templatable {
'isactive' => $isactive,
'url' => $isactive ? new \moodle_url('#') : new \moodle_url($this->page->url, ['lang' => $langtype]),
];
if (!empty($attributes)) {
$node['attributes'] = $attributes;
}
$nodes[] = $node;
@ -135,8 +146,15 @@ class language_menu implements \renderable, \templatable {
}
$langmenu->set_menu_trigger($menuname);
foreach ($languagedata['items'] as $node) {
$lang = new \action_menu_link_secondary($node['url'], null, $node['title'],
['data-lang' => $node['url']->get_param('lang')]);
$langparam = $node['url']->get_param('lang');
$attributes = [];
if ($langparam) {
$attributes = [
'data-lang' => $langparam,
'lang' => $langparam,
];
}
$lang = new \action_menu_link_secondary($node['url'], null, $node['title'], $attributes);
$langmenu->add($lang);
}
return $langmenu->export_for_template($output);

View File

@ -41,7 +41,8 @@
}}
{{#items}}
{{#link}}
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" tabindex="-1" {{#isactive}}aria-current="true"{{/isactive}}>
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" tabindex="-1" {{#isactive}}aria-current="true"{{/isactive}}
{{#attributes}}{{key}}="{{value}}" {{/attributes}}>
{{text}}
</a>
{{/link}}

View File

@ -72,10 +72,19 @@ class language_menu_test extends \advanced_testcase {
// Assert that the number of language menu items matches the number of the expected items.
$this->assertEquals(count($expected['items']), count($response['items']));
foreach ($expected['items'] as $expecteditem) {
$lang = $expecteditem['lang'];
// We need to manually generate the url key and its value in the expected item array as this cannot
// be done in the data provider due to the change of the state of $PAGE.
$expecteditem['url'] = $expecteditem['isactive'] ? new \moodle_url('#') :
new \moodle_url($PAGE->url, ['lang' => $expecteditem['lang']]);
if ($expecteditem['isactive']) {
$expecteditem['url'] = new \moodle_url('#');
} else {
$expecteditem['url'] = new \moodle_url($PAGE->url, ['lang' => $lang]);
// When the language menu item is not the current language, it will contain the lang attribute.
$expecteditem['attributes'][] = [
'key' => 'lang',
'value' => $lang
];
}
// The lang value is only used to generate the url, so this key can be removed.
unset($expecteditem['lang']);

View File

@ -53,7 +53,8 @@
<div role="menu" aria-labelledby="lang-menu-toggle" id="lang-action-menu" class="dropdown-menu dropdown-menu-right">
{{#items}}
{{#link}}
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" {{#isactive}}aria-current="true"{{/isactive}}>
<a href="{{{url}}}" class="dropdown-item pl-5" role="menuitem" {{#isactive}}aria-current="true"{{/isactive}}
{{#attributes}}{{key}}="{{value}}" {{/attributes}}>
{{text}}
</a>
{{/link}}