Anyone know how to programatically add keyvalue pairs to a KeyValue property editor from the Key/Value Editor package? I’m using the following but don’t even get the “Add” box available if I use it. I do if I comment the coursePage.Properties line below, but that defeats the object…
// modules is a Key/value editor from the package set up to handle 20 pairs.
coursePage.Properties["modules"]?.SetValue(await GetModules(courseRows, "Module"));
Task<List<Vokseverk.KeyValuePropertyConverter.KeyAndValue>?> GetModules(List<CourseImportRow> courseRows, string what) {
List<Vokseverk.KeyValuePropertyConverter.KeyAndValue> foundModules = new List<Vokseverk.KeyValuePropertyConverter.KeyAndValue>();
foreach (CourseImportRow row in courseRows) {
if (string.Equals(row.CourseInfoType?.Trim(), what, StringComparison.CurrentCultureIgnoreCase)) {
string? rawModule = row?.CourseInfoText?.Trim();
var match = Regex.Match(rawModule, @"\[(.*?)\]");
string? moduleTitle = "";
string? moduleDescription = "";
if (match.Success) {
moduleTitle = match.Groups[1].Value;
moduleDescription = rawModule.Replace(match.Value, "").Trim() == "" ? "None" : rawModule.Replace(match.Value, "").Trim();
}
Vokseverk.KeyValuePropertyConverter.KeyAndValue newModule =
new Vokseverk.KeyValuePropertyConverter.KeyAndValue(
key: moduleTitle,
value: moduleDescription
);
foundModules.Add(newModule);
}
}
return Task.FromResult(foundModules);
}
When run, the return foundModules has 7 correctly populated items, but nothing appears in the Node.
I’m the author of said PropertyEditor and I have no idea how to do that
That said, it was heavily based on the built-in Repeatable TextStrings property editor, so maybe you can figure something out. It’s very likely that I didn’t implement something properly (e.g. the PropertyEditorValueConverter) to support that.
More than happy to accept a PR if you (or someone else) figure out what’s missing!
I’m using your first method, and method returns what looks like a well populated dictionary item, but the issue may be with how I’m saving it to the content node which is:-
I mean, is it legal to “await” the execution of a method while doing the SetValue(…)?
I don’t think so, no — I think the whole thing has to be labelled async if you use await somewhere inside… so it’s very likely that that’s the problem right there; the values are being created just fine, but the node has already been saved when the method returns with them, so to speak.
I removed the await, and it didn’t make any difference. I might have to ditch this approach and do string manipulation instead. I get strings like “[this is a title]this is some text” which I was hoping to save as key/value, but I’ll just have to add the whole thing to a repeatable textstring and manipulate it in the view every time it’s displayed.