From 7ef7aaecac7def504e360a6e6fc5f9e132a2fa00 Mon Sep 17 00:00:00 2001 From: Jun Pataleta Date: Thu, 27 Aug 2020 13:17:45 +0800 Subject: [PATCH] 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 e08b3d3ab97..b6277cb1cf8 100644 --- a/lib/outputcomponents.php +++ b/lib/outputcomponents.php @@ -4682,6 +4682,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. * @@ -4705,10 +4711,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 = [];