From 8305c3ce9a8ad96d68cede9edd38d7fe8e3e2915 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 27 Aug 2020 13:17:45 +0800 Subject: [PATCH 1/2] MDL-69262 core: Make instance count variable as a static class variable With the static function variable $instance, calls to \action_menu_link::export_for_template() from its subclasses are stored in different variables. This causes duplicate IDs when different implementations of action menu links are rendered on the action menu trigger/link template. (e.g. action_menu_link and action_menu_link_secondary both rendered on the same page). To make the incrementing uniform for the action_menu_link class and its implementations, the $instance variable is moved out of the export_to_template() method and is now made to a static class variable. --- lib/outputcomponents.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/outputcomponents.php b/lib/outputcomponents.php index 08e691c6530..bc1f002af48 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -4679,6 +4679,12 @@ class action_menu_link extends action_link implements renderable { */ public $actionmenu = null; + /** + * The number of instances of this action menu link (and its subclasses). + * @var int + */ + protected static $instance = 1; + /** * Constructs the object. * @@ -4702,10 +4708,8 @@ class action_menu_link extends action_link implements renderable { * @return stdClass */ public function export_for_template(renderer_base $output) { - static $instance = 1; - $data = parent::export_for_template($output); - $data->instance = $instance++; + $data->instance = self::$instance++; // Ignore what the parent did with the attributes, except for ID and class. $data->attributes = []; From bb41dd792180cf6d000f5263f9149acf98ed1b2a Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 27 Aug 2020 13:23:24 +0800 Subject: [PATCH 2/2] MDL-69262 core: Use core/action_menu_link in core/action_menu_trigger * Introduce the "actionmenulinkclasses" block in the action_menu_link template so other templates importing it can have the option to override its classes. * Have the action menu trigger template use the action menu link template to reduce code duplication. --- lib/templates/action_menu_link.mustache | 9 ++++++++- lib/templates/action_menu_trigger.mustache | 13 +++---------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/templates/action_menu_link.mustache b/lib/templates/action_menu_link.mustache index 0e9f42af6cd..67affd7429f 100644 --- a/lib/templates/action_menu_link.mustache +++ b/lib/templates/action_menu_link.mustache @@ -27,7 +27,14 @@ } }} {{^disabled}} - {{#icon}}{{#pix}}{{key}}, {{component}}, {{title}}{{/pix}}{{/icon}}{{#showtext}}{{{text}}}{{/showtext}} + + {{#icon}} + {{#pix}}{{key}}, {{component}}, {{title}}{{/pix}} + {{/icon}} + {{#showtext}} + {{{text}}} + {{/showtext}} + {{/disabled}} {{#disabled}} {{#icon}}{{#pix}}{{key}},{{component}},{{title}}{{/pix}}{{/icon}}{{{text}}} diff --git a/lib/templates/action_menu_trigger.mustache b/lib/templates/action_menu_trigger.mustache index cf34a04a6f6..93f483fcde7 100644 --- a/lib/templates/action_menu_trigger.mustache +++ b/lib/templates/action_menu_trigger.mustache @@ -95,16 +95,9 @@