| @@ -200,8 +200,27 @@ | ||
| 200 | 200 | value, |
| 201 | 201 | }); |
| 202 | 202 | } |
| 203 | 203 | |
| 204 | + // If the block's saved value references a legacy form that | |
| 205 | + // isn't in field.values (legacy forms are not offered as | |
| 206 | + // new selection choices), append it here so the dropdown | |
| 207 | + // still reflects the saved selection. | |
| 208 | + if ( | |
| 209 | + fieldProperties.value && | |
| 210 | + field.legacy_values && | |
| 211 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 212 | + 'undefined' && | |
| 213 | + !fieldOptions.some( | |
| 214 | + (option) => option.value === fieldProperties.value | |
| 215 | + ) | |
| 216 | + ) { | |
| 217 | + fieldOptions.push({ | |
| 218 | + label: field.legacy_values[fieldProperties.value], | |
| 219 | + value: fieldProperties.value, | |
| 220 | + }); | |
| 221 | + } | |
| 222 | + | |
| 204 | 223 | // Sort field's options alphabetically by label. |
| 205 | 224 | fieldOptions.sort(function (x, y) { |
| 206 | 225 | const a = x.label.toUpperCase(), |
| 207 | 226 | b = y.label.toUpperCase(); |
| @@ -226,8 +245,27 @@ | ||
| 226 | 245 | value, |
| 227 | 246 | }); |
| 228 | 247 | } |
| 229 | 248 | |
| 249 | + // If the block's saved value references a legacy form that | |
| 250 | + // isn't in field.values (because legacy forms are not | |
| 251 | + // offered as new selection choices), append it here so the | |
| 252 | + // dropdown still reflects the saved selection. | |
| 253 | + if ( | |
| 254 | + fieldProperties.value && | |
| 255 | + field.legacy_values && | |
| 256 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 257 | + 'undefined' && | |
| 258 | + !fieldOptions.some( | |
| 259 | + (option) => option.value === fieldProperties.value | |
| 260 | + ) | |
| 261 | + ) { | |
| 262 | + fieldOptions.push({ | |
| 263 | + label: field.legacy_values[fieldProperties.value], | |
| 264 | + value: fieldProperties.value, | |
| 265 | + }); | |
| 266 | + } | |
| 267 | + | |
| 230 | 268 | // Sort field's options alphabetically by label. |
| 231 | 269 | fieldOptions.sort(function (x, y) { |
| 232 | 270 | const a = x.label.toUpperCase(), |
| 233 | 271 | b = y.label.toUpperCase(); |
| @@ -1146,8 +1184,9 @@ | ||
| 1146 | 1184 | ); |
| 1147 | 1185 | |
| 1148 | 1186 | if (hasOptgroups) { |
| 1149 | 1187 | const children = []; |
| 1188 | + const seenValues = new Set(); | |
| 1150 | 1189 | |
| 1151 | 1190 | for (const value of Object.keys(field.values)) { |
| 1152 | 1191 | if ( |
| 1153 | 1192 | typeof field.values[value] === 'object' && |
| @@ -1158,8 +1197,9 @@ | ||
| 1158 | 1197 | const groupChildren = []; |
| 1159 | 1198 | for (const groupValue of Object.keys( |
| 1160 | 1199 | field.values[value].values |
| 1161 | 1200 | )) { |
| 1201 | + seenValues.add(groupValue); | |
| 1162 | 1202 | groupChildren.push( |
| 1163 | 1203 | el( |
| 1164 | 1204 | 'option', |
| 1165 | 1205 | { |
| @@ -1181,8 +1221,9 @@ | ||
| 1181 | 1221 | ) |
| 1182 | 1222 | ); |
| 1183 | 1223 | } else { |
| 1184 | 1224 | // Option within optgroup. |
| 1225 | + seenValues.add(value); | |
| 1185 | 1226 | children.push( |
| 1186 | 1227 | el( |
| 1187 | 1228 | 'option', |
| 1188 | 1229 | { value, key: value }, |
| @@ -1191,8 +1232,30 @@ | ||
| 1191 | 1232 | ); |
| 1192 | 1233 | } |
| 1193 | 1234 | } |
| 1194 | 1235 | |
| 1236 | + // If the saved value references a legacy form that isn't | |
| 1237 | + // otherwise in the dropdown, append it so the current | |
| 1238 | + // selection remains visible. | |
| 1239 | + if ( | |
| 1240 | + fieldProperties.value && | |
| 1241 | + field.legacy_values && | |
| 1242 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 1243 | + 'undefined' && | |
| 1244 | + !seenValues.has(fieldProperties.value) | |
| 1245 | + ) { | |
| 1246 | + children.push( | |
| 1247 | + el( | |
| 1248 | + 'option', | |
| 1249 | + { | |
| 1250 | + value: fieldProperties.value, | |
| 1251 | + key: fieldProperties.value, | |
| 1252 | + }, | |
| 1253 | + field.legacy_values[fieldProperties.value] | |
| 1254 | + ) | |
| 1255 | + ); | |
| 1256 | + } | |
| 1257 | + | |
| 1195 | 1258 | return el(SelectControl, fieldProperties, ...children); |
| 1196 | 1259 | } |
| 1197 | 1260 | |
| 1198 | 1261 | // Options only, no optgroups. |
| @@ -1200,8 +1263,26 @@ | ||
| 1200 | 1263 | for (const value of Object.keys(field.values)) { |
| 1201 | 1264 | fieldOptions.push({ |
| 1202 | 1265 | label: field.values[value], |
| 1203 | 1266 | value, |
| 1267 | + }); | |
| 1268 | + } | |
| 1269 | + | |
| 1270 | + // If the saved value references a legacy form that isn't in | |
| 1271 | + // field.values, append it so the current selection remains | |
| 1272 | + // visible in the dropdown. | |
| 1273 | + if ( | |
| 1274 | + fieldProperties.value && | |
| 1275 | + field.legacy_values && | |
| 1276 | + typeof field.legacy_values[fieldProperties.value] !== | |
| 1277 | + 'undefined' && | |
| 1278 | + !fieldOptions.some( | |
| 1279 | + (option) => option.value === fieldProperties.value | |
| 1280 | + ) | |
| 1281 | + ) { | |
| 1282 | + fieldOptions.push({ | |
| 1283 | + label: field.legacy_values[fieldProperties.value], | |
| 1284 | + value: fieldProperties.value, | |
| 1204 | 1285 | }); |
| 1205 | 1286 | } |
| 1206 | 1287 | |
| 1207 | 1288 | // Sort options alphabetically by label. |