Last active
January 11, 2023 12:53
-
-
Save smdooley/1d37a274adb26688e0754550b0ae5166 to your computer and use it in GitHub Desktop.
Using Umbraco notifications with a Block List Editor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Umbraco.Cms.Core.Events; | |
using Umbraco.Cms.Core.Models.Blocks; | |
using Umbraco.Cms.Core.Notifications; | |
namespace Concept.Core.NotificationHandlers | |
{ | |
public class ContentPublishing : INotificationHandler<ContentPublishingNotification> | |
{ | |
public void Handle(ContentPublishingNotification notification) | |
{ | |
foreach (var node in notification.PublishedEntities) | |
{ | |
var publishingCultures = node | |
.AvailableCultures | |
.Where(culture => notification.IsPublishingCulture(node, culture)).ToList(); | |
var unPublishingCultures = node | |
.AvailableCultures | |
.Where(culture => notification.IsUnpublishingCulture(node, culture)).ToList(); | |
if (node.ContentType.Alias.Equals("standardPage")) | |
{ | |
foreach (var publishedCulture in publishingCultures) | |
{ | |
var rawJson = node.GetValue("blocks", publishedCulture)?.ToString(); | |
if (string.IsNullOrWhiteSpace(rawJson)) continue; | |
var blockValue = Newtonsoft.Json.JsonConvert.DeserializeObject<BlockValue>(rawJson); | |
if (blockValue is null) continue; | |
foreach (var item in blockValue.SettingsData) | |
{ | |
foreach (var itemProperty in item.RawPropertyValues) | |
{ | |
... | |
} | |
} | |
foreach (var item in blockValue.ContentData) | |
{ | |
foreach (var itemProperty in item.RawPropertyValues) | |
{ | |
if (itemProperty.Key == "showWarning") | |
{ | |
notification.CancelOperation(new EventMessage("Warning", "Invalid configuration", EventMessageType.Error)); | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment