From a986cb4ba143570f6402abfb3b1757c2c32cae45 Mon Sep 17 00:00:00 2001 From: Damyon Wiese Date: Mon, 3 Sep 2018 15:08:38 +0800 Subject: [PATCH] MDL-62144 user menu: Accessibility Ensure user menu passes the accessibility tests. --- lib/outputcomponents.php | 30 ++++++++++++++++++++++++++++++ lib/outputrenderers.php | 1 + 2 files changed, 31 insertions(+) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index d5f45acb9c3..772385e2f43 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -4133,6 +4133,12 @@ class action_menu implements renderable, templatable { */ public $actiontext = null; + /** + * The string to use for the accessible label for the menu. + * @var array + */ + public $actionlabel = null; + /** * An icon to use for the toggling the secondary menu (dropdown). * @var actionicon @@ -4190,6 +4196,16 @@ class action_menu implements renderable, templatable { } } + /** + * Sets the label for the menu trigger. + * + * @param string $label The text + * @return null + */ + public function set_action_label($label) { + $this->actionlabel = $label; + } + /** * Sets the menu trigger text. * @@ -4312,10 +4328,17 @@ class action_menu implements renderable, templatable { if ($this->actiontext) { $string = $this->actiontext; } + $label = ''; + if ($this->actionlabel) { + $label = $this->actionlabel; + } else { + $label = $title; + } $actions = $this->primaryactions; $attributes = array( 'class' => implode(' ', $linkclasses), 'title' => $title, + 'aria-label' => $label, 'id' => 'action-menu-toggle-'.$this->instance, 'role' => 'menuitem' ); @@ -4481,6 +4504,13 @@ class action_menu implements renderable, templatable { if (!empty($this->menutrigger)) { $primary->menutrigger = $this->menutrigger; $primary->triggerextraclasses = $this->triggerextraclasses; + if ($this->actionlabel) { + $primary->title = $this->actionlabel; + } else if ($this->actiontext) { + $primary->title = $this->actiontext; + } else { + $primary->title = strip_tags($this->menutrigger); + } } else { $primary->title = get_string('actionsmenu'); $iconattributes = ['class' => 'iconsmall actionmenu', 'title' => $primary->title]; diff --git a/lib/outputrenderers.php b/lib/outputrenderers.php index a0748576b64..8645b6b11c5 100644 --- a/lib/outputrenderers.php +++ b/lib/outputrenderers.php @@ -3405,6 +3405,7 @@ EOD; $am->set_menu_trigger( $returnstr ); + $am->set_action_label(get_string('usermenu')); $am->set_alignment(action_menu::TR, action_menu::BR); $am->set_nowrap_on_items(); if ($withlinks) {