Skip to content

Commit

Permalink
MDL-72885 output: Allow additional attributes for custom menu items
Browse files Browse the repository at this point in the history
* Plus don't render the link title if it's the same as the link text.
  • Loading branch information
junpataleta committed May 27, 2022
1 parent b81a2f8 commit af1936f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
22 changes: 18 additions & 4 deletions lib/outputcomponents.php
Expand Up @@ -3487,6 +3487,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
*
Expand All @@ -3496,13 +3499,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;
}

/**
Expand All @@ -3512,14 +3518,15 @@ public function __construct($text, moodle_url $url=null, $title=null, $sort = nu
* @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];
}
Expand Down Expand Up @@ -3652,8 +3659,15 @@ public function export_for_template(renderer_base $output) {
$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;
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/custom_menu_item.mustache
Expand Up @@ -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>
Expand Down

0 comments on commit af1936f

Please sign in to comment.