Merge branch 'MDL-72885-311' of https://github.com/junpataleta/moodle into MOODLE_311_STABLE

This commit is contained in:
Víctor Déniz
2022-06-29 22:47:49 +01:00
3 changed files with 31 additions and 9 deletions
+18 -4
View File
@@ -3480,6 +3480,9 @@ class custom_menu_item implements renderable, templatable {
*/
protected $lastsort = 0;
/** @var array Array of other HTML attributes for the custom menu item. */
protected $attributes = [];
/**
* Constructs the new custom menu item
*
@@ -3489,13 +3492,16 @@ class custom_menu_item implements renderable, templatable {
* @param int $sort A sort or to use if we need to sort differently [Optional]
* @param custom_menu_item $parent A reference to the parent custom_menu_item this child
* belongs to, only if the child has a parent. [Optional]
* @param array $attributes Array of other HTML attributes for the custom menu item.
*/
public function __construct($text, moodle_url $url=null, $title=null, $sort = null, custom_menu_item $parent = null) {
public function __construct($text, moodle_url $url = null, $title = null, $sort = null, custom_menu_item $parent = null,
array $attributes = []) {
$this->text = $text;
$this->url = $url;
$this->title = $title;
$this->sort = (int)$sort;
$this->parent = $parent;
$this->attributes = $attributes;
}
/**
@@ -3505,14 +3511,15 @@ class custom_menu_item implements renderable, templatable {
* @param moodle_url $url
* @param string $title
* @param int $sort
* @param array $attributes Array of other HTML attributes for the custom menu item.
* @return custom_menu_item
*/
public function add($text, moodle_url $url = null, $title = null, $sort = null) {
public function add($text, moodle_url $url = null, $title = null, $sort = null, $attributes = []) {
$key = count($this->children);
if (empty($sort)) {
$sort = $this->lastsort + 1;
}
$this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this);
$this->children[$key] = new custom_menu_item($text, $url, $title, $sort, $this, $attributes);
$this->lastsort = (int)$sort;
return $this->children[$key];
}
@@ -3645,8 +3652,15 @@ class custom_menu_item implements renderable, templatable {
$context = new stdClass();
$context->text = external_format_string($this->text, $syscontext->id);
$context->url = $this->url ? $this->url->out() : null;
$context->title = external_format_string($this->title, $syscontext->id);
// No need for the title if it's the same with text.
if ($this->text !== $this->title) {
// Show the title attribute only if it's different from the text.
$context->title = external_format_string($this->title, $syscontext->id);
}
$context->sort = $this->sort;
if (!empty($this->attributes)) {
$context->attributes = $this->attributes;
}
$context->children = array();
if (preg_match("/^#+$/", $this->text)) {
$context->divider = true;
+12 -4
View File
@@ -3741,13 +3741,21 @@ EOD;
$strlang = get_string('language');
$currentlang = current_language();
if (isset($langs[$currentlang])) {
$currentlang = $langs[$currentlang];
$currentlangstr = $langs[$currentlang];
} else {
$currentlang = $strlang;
$currentlangstr = $strlang;
}
$this->language = $menu->add($currentlang, new moodle_url('#'), $strlang, 10000);
$this->language = $menu->add($currentlangstr, new moodle_url('#'), $strlang, 10000);
foreach ($langs as $langtype => $langname) {
$this->language->add($langname, new moodle_url($this->page->url, array('lang' => $langtype)), $langname);
$attributes = [];
// Set the lang attribute for languages different from the page's current language.
if ($langtype !== $currentlang) {
$attributes[] = [
'key' => 'lang',
'value' => str_replace('_', '-', $langtype),
];
}
$this->language->add($langname, new moodle_url($this->page->url, ['lang' => $langtype]), null, null, $attributes);
}
}
+1 -1
View File
@@ -39,7 +39,7 @@
<div class="dropdown-menu" role="menu" id="drop-down-menu-{{uniqid}}" aria-labelledby="drop-down-{{uniqid}}">
{{#children}}
{{^divider}}
<a class="dropdown-item" role="menuitem" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}}>{{{text}}}</a>
<a class="dropdown-item" role="menuitem" href="{{{url}}}" {{#title}}title="{{{title}}}"{{/title}} {{#attributes}}{{key}}="{{value}}" {{/attributes}}>{{{text}}}</a>
{{/divider}}
{{#divider}}
<div class="dropdown-divider" role="presentation"></div>