Differences Between: [Versions 400 and 402] [Versions 401 and 402]
1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Contains class \core\output\language_menu 19 * 20 * @package core 21 * @category output 22 * @copyright 2021 Adrian Greeve <adrian@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core\output; 27 28 /** 29 * Class for creating the language menu 30 * 31 * @package core 32 * @category output 33 * @copyright 2021 Adrian Greeve <adrian@moodle.com> 34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 35 */ 36 class language_menu implements \renderable, \templatable { 37 38 /** @var \moodle_page $page the moodle page that the languague menu belongs to */ 39 protected $page; 40 41 /** @var string current language code */ 42 protected $currentlang; 43 44 /** @var array localised list of installed translations */ 45 protected $langs; 46 47 /** 48 * Language menu constructor. 49 * 50 * @param \moodle_page $page the moodle page that the languague menu belongs to. 51 */ 52 public function __construct($page) { 53 $this->page = $page; 54 $this->currentlang = \current_language(); 55 $this->langs = \get_string_manager()->get_list_of_translations(); 56 } 57 58 /** 59 * Determine if the language menu should be shown. 60 * 61 * @return bool true if the language menu should be shown. 62 */ 63 protected function show_language_menu(): bool { 64 global $CFG; 65 66 if (empty($CFG->langmenu)) { 67 return false; 68 } 69 70 if ($this->page->course != SITEID and !empty($this->page->course->lang)) { 71 // Do not show lang menu if language forced. 72 return false; 73 } 74 75 if (count($this->langs) < 2) { 76 return false; 77 } 78 return true; 79 } 80 81 /** 82 * Export the data. 83 * 84 * @param \renderer_base $output 85 * @return array with the title for the menu and an array of items. 86 */ 87 public function export_for_template(\renderer_base $output): array { 88 // Early return if a lang menu does not exists. 89 if (!$this->show_language_menu()) { 90 return []; 91 } 92 93 $nodes = []; 94 $activelanguage = ''; 95 96 // Add the lang picker if needed. 97 foreach ($this->langs as $langtype => $langname) { 98 $isactive = $langtype == $this->currentlang; 99 $attributes = []; 100 if (!$isactive) { 101 // Set the lang attribute for languages different from the page's current language. 102 $attributes[] = [ 103 'key' => 'lang', 104 'value' => get_html_lang_attribute_value($langtype), 105 ]; 106 } 107 $node = [ 108 'title' => $langname, 109 'text' => $langname, 110 'link' => true, 111 'isactive' => $isactive, 112 'url' => $isactive ? new \moodle_url('#') : new \moodle_url($this->page->url, ['lang' => $langtype]), 113 ]; 114 if (!empty($attributes)) { 115 $node['attributes'] = $attributes; 116 } 117 118 $nodes[] = $node; 119 120 if ($isactive) { 121 $activelanguage = $langname; 122 } 123 } 124 125 return [ 126 'title' => $activelanguage, 127 'items' => $nodes, 128 ]; 129 } 130 131 /** 132 * Export the data providing a structure for the core/action_menu template. 133 * 134 * @param \renderer_base $output 135 * @return \stdClass action_menu data export. 136 */ 137 public function export_for_action_menu(\renderer_base $output): ?\stdClass { 138 $languagedata = $this->export_for_template($output); 139 if (empty($languagedata)) { 140 return null; 141 } 142 $langmenu = new \action_menu(); 143 $menuname = \get_string('language'); 144 if (!empty($languagedata['title'])) { 145 $menuname = $languagedata['title']; 146 } 147 $langmenu->set_menu_trigger($menuname); 148 foreach ($languagedata['items'] as $node) { 149 $langparam = $node['url']->get_param('lang'); 150 $attributes = []; 151 if ($langparam) { 152 $attributes = [ 153 'data-lang' => $langparam, 154 'lang' => get_html_lang_attribute_value($langparam), 155 ]; 156 } 157 $lang = new \action_menu_link_secondary($node['url'], null, $node['title'], $attributes); 158 $langmenu->add($lang); 159 } 160 return $langmenu->export_for_template($output); 161 } 162 163 /** 164 * Export the data providing a structure for the core/single_select template. 165 * 166 * @param \renderer_base $output 167 * @return \stdClass single_select data export. 168 */ 169 public function export_for_single_select(\renderer_base $output): ?\stdClass { 170 if (!$this->show_language_menu()) { 171 return null; 172 } 173 $singleselect = new \single_select($this->page->url, 'lang', $this->langs, $this->currentlang, null); 174 $singleselect->label = get_accesshide(\get_string('language')); 175 $singleselect->class = 'langmenu'; 176 return $singleselect->export_for_template($output); 177 } 178 179 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body