Skip to content

Commit 9fbdb0b

Browse files
Bugfix: Static singleton needs component and area in keys.
1 parent aafd451 commit 9fbdb0b

1 file changed

Lines changed: 21 additions & 5 deletions

File tree

classes/local/customfield/wbt_field_controller_info.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,17 @@ public static function instantiate_by_shortnames(array $shortnames, string $comp
100100
$records = $DB->get_records_sql($sql, $inparams);
101101

102102
foreach ($records as $record) {
103+
$key = $record->shortname;
104+
if (!empty($area)) {
105+
$key = "$area-$key";
106+
}
107+
if (!empty($component)) {
108+
$key = "$component-$key";
109+
}
103110
// Only take the first (newest) instance per shortname.
104-
if (!isset(self::$instances[$record->shortname])) {
111+
if (!isset(self::$instances[$key])) {
105112
if ($instance = self::create($record)) {
106-
self::$instances[$record->shortname] = $instance;
113+
self::$instances[$key] = $instance;
107114
}
108115
}
109116
}
@@ -118,8 +125,17 @@ public static function instantiate_by_shortnames(array $shortnames, string $comp
118125
* @return wbt_field_controller_base the field controller for the customfield record
119126
*/
120127
public static function get_instance_by_shortname(string $shortname, string $component = '', string $area = '') {
121-
if (!empty(self::$instances[$shortname])) {
122-
return self::$instances[$shortname];
128+
// Key for static singleton.
129+
$key = $shortname;
130+
if (!empty($area)) {
131+
$key = "$area-$key";
132+
}
133+
if (!empty($component)) {
134+
$key = "$component-$key";
135+
}
136+
137+
if (!empty(self::$instances[$key])) {
138+
return self::$instances[$key];
123139
} else {
124140
global $DB;
125141

@@ -144,7 +160,7 @@ public static function get_instance_by_shortname(string $shortname, string $comp
144160

145161
if ($record = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
146162
if ($instance = self::create($record)) {
147-
self::$instances[$record->shortname] = $instance;
163+
self::$instances[$key] = $instance;
148164
return $instance;
149165
}
150166
}

0 commit comments

Comments
 (0)