Tom Chance
08/22/2023, 8:39 AMcsharp
foreach (var palette in palettes)
{
var paletteName = palette.Name;
var paletteId = palette.Key;
umbDocDictionary.Add(paletteName, paletteId);
}
foreach (var node in allNodes)
{
if (node.HasProperty("sectionPicker") && node.HasValue("sectionPicker"))
{
var sectionPicker = node.Value<IEnumerable<BlockListItem>>("sectionPicker");
var contentNode = _contentService.GetById(node.Id);
var jsonValue = contentNode.GetValue("sectionPicker").ToString();
BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(jsonValue);
foreach (var settingObj in blockValue.SettingsData)
{
foreach (var settingColumnProperty in settingObj.RawPropertyValues)
{
var paletteValue = "";
if (settingColumnProperty.Key == "backgroundColor")
{
var colorObj = settingColumnProperty.Value.ToString();
var color = JsonConvert.DeserializeObject<ColorPickerValueConverter.PickedColor>(colorObj);
var neededPalette = (color.Label == "Default") ? umbDocDictionary["Palette Default"] : umbDocDictionary[color.Label];
paletteValue = JsonConvert.SerializeObject(new[] { Udi.Create(Constants.UdiEntityType.Document, neededPalette).UriValue });
}
}
}
}
}
Ultimately, this has given me the JSON of the BlockList, Allowed me to loop through the each of the blocklist settings nodes, detect whether they have a key with the name "backgroundColor" which is the label of a color picker. Then correctly create the UDI needed to "set" in the other property which is a link (palette).
Now i've hit the roadblock of actually "setting" this.
Reading the docs at : https://github.com/umbraco/UmbracoDocs/blob/e64ec0e5b28b4e5a37b7865691621e45dd82701f/Getting-Started/Backoffice/Property-Editors/Built-in-Property-Editors/Block-List-Editor/index.md
I see the best way is to create a custom model that is decorated with the Umbraco.BlockList Jsonproperty to match the Json structure.
My question ultimately rounds out to: Do i need to mock up the entire settings model? Or can i somehow target just the individual property? (Simply due to having a number of different BlockList settings document types across the site.)
Sorry that its a bit of a mouthful of a question 😅Mike Chambers
08/22/2023, 9:59 AMTom Chance
08/22/2023, 10:06 AMcsharp
column.RawPropertyValues["palette"] = JsonConvert.SerializeObject(paletteValue);
Would doMike Chambers
08/22/2023, 10:22 AMcsharp
var settingNodes = j.SelectToken($"$..settingsData[?(@.udi=={settingsUdi})]");
if (settingNodes.FirstOrDefault() is JObject setting)
{
setting["palette"] = JsonConvert.SerializeObject(paletteValue);
}
eg from the blockgrid example
json
{
"layout": {
"Umbraco.BlockGrid": [{
"contentUdi": "umb://element/bb23fe28160941efa506da7aa314172d",
"settingsUdi": "umb://element/9b832ee528464456a8e9a658b47a9801",
"areas": [],
"columnSpan": 12,
"rowSpan": 1
}, {
"contentUdi": "umb://element/a11e06ca155d40b78189be0bdaf11c6d",
"settingsUdi": "umb://element/d182a0d807fc4518b741b77c18aa73a1",
"areas": [],
"columnSpan": 6,
"rowSpan": 2
}
]
},
"contentData": [{
"contentTypeKey": "0e9f8609-1904-4fd1-9801-ad1880825ff3",
"udi": "umb://element/bb23fe28160941efa506da7aa314172d",
"title": "Item one",
"text": "This is item one"
}, {
"contentTypeKey": "0e9f8609-1904-4fd1-9801-ad1880825ff3",
"udi": "umb://element/a11e06ca155d40b78189be0bdaf11c6d",
"title": "Item two",
"text": "This is item two"
}
],
"settingsData": [{
"contentTypeKey": "22be457c-8249-42b8-8685-d33262f7ce2a",
"udi": "umb://element/9b832ee528464456a8e9a658b47a9801",
"featured": "0"
}, {
"contentTypeKey": "22be457c-8249-42b8-8685-d33262f7ce2a",
"udi": "umb://element/d182a0d807fc4518b741b77c18aa73a1",
"featured": "1"
}
]
}
$..settingsData[?(@.udi=='umb://element/9b832ee528464456a8e9a658b47a9801')]
array of 1 for first contentdata.
I find https://jsonpath.com/ really useful for testing.Dean Leigh
08/22/2023, 10:23 AM@{
if (Model?.Areas.Any() != true) { return; }
var hasSettings = Model.Settings != null;
var backgroundColour = hasSettings ? Model.Settings.Value<ColorPickerValueConverter.PickedColor>("layoutSettingsColourPicker") : null;
var colorLabel = backgroundColour?.Label;
var colorShades = hasSettings ? Model.Settings.Value<decimal>("layoutSettingsColourShades") : (decimal?)null;
var backgroundImage = hasSettings ? Model.Settings.Value<Umbraco.Cms.Core.Models.MediaWithCrops>("layoutSettingsBackgroundImagePicker") : null;
}
@{
string styleAttribute = "";
if (hasSettings && backgroundColour != null)
{
styleAttribute = $"background-color: var({colorLabel}";
if (colorShades.HasValue)
{
styleAttribute += $"-{colorShades});";
}
}
else if (backgroundImage != null)
{
styleAttribute = $"background-image: url({backgroundImage.MediaUrl()});";
}
}
<div class="layout py-3" data-bgimage="@backgroundImage.MediaUrl()" style="@styleAttribute background-size: contain;" data-block-alias="@Model.Content.ContentType.Alias">
@RenderBody()
</div>
Dean Leigh
08/22/2023, 10:24 AMTom Chance
08/22/2023, 10:32 AMTom Chance
08/22/2023, 10:33 AMDean Leigh
08/22/2023, 10:35 AMTom Chance
08/22/2023, 10:37 AMMike Chambers
08/22/2023, 10:37 AMDean Leigh
08/22/2023, 10:41 AMMike Chambers
08/22/2023, 10:45 AMTom Chance
08/22/2023, 11:03 AMSebastiaan
08/22/2023, 11:04 AMSebastiaan
08/22/2023, 11:06 AMIUmbracoContextFactory
https://cultiv.nl/blog/using-hangfire-to-update-umbraco-content/Dean Leigh
08/22/2023, 11:38 AMTom Chance
08/23/2023, 12:46 PM