Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Markus Johansson 1911 posts 5757 karma points MVP c-trib
    Apr 04, 2022 @ 14:50
    Markus Johansson
    0

    Update content of "Block" inside Block List on save

    I'm trying to update the content of a Rich Text Editor inside a blocklist using a "ContentSavingNotification"-handler `INotificationHandler

    I've managed to get my hands on the content but the changes that I make are not persisted.

    I looks something like this:

    foreach (var entity in notification.SavedEntities) {

    foreach (var property in entity.Properties)
    {
        if (property.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
        {
    
            string rawJson = property.GetValue().ToString();
    
            BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);
    
            foreach (var column in blockValue.ContentData)
            {
                foreach (var columnProperty in column.RawPropertyValues)
                {
                    // this contains other blocks
                    var block = JsonConvert.DeserializeObject<BlockValue>(columnProperty.Value.ToString());
    
                    if (block == null)
                        continue;
    
                    foreach (var blockData in block.ContentData)
                    {
                        foreach (var blockProperty in blockData.RawPropertyValues)
                        {
                            if (blockProperty.Key == "body")
                            {
                                blockData.RawPropertyValues["body"] =
                                    new HtmlInputCleaner().CleanUp(blockProperty.Value.ToString());
                            }
                        }
                    }
    
                }
            }
        }
    }
    

    }

    Anyone that has managed to do this? I guess the process is simular for Nested Content and the Block List Editor?

  • Markus Johansson 1911 posts 5757 karma points MVP c-trib
    Apr 04, 2022 @ 15:04
    Markus Johansson
    100

    Ohh... I forgot =D Any changes that I make to the object graph after DeSerializing to Json needs to be serialized back again =D

    Here is a working example:

    public class RichTextEditorCleanupOnSaveHandler : 
    INotificationHandler<ContentSavingNotification>
    {
    public void Handle(ContentSavingNotification notification)
    {
    
        foreach (var entity in notification.SavedEntities)
        {
    
            foreach (var property in entity.Properties)
            {
                if (property.PropertyType.PropertyEditorAlias == "Umbraco.BlockList")
                {
    
                    string rawJson = property.GetValue().ToString();
    
                    BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(rawJson);
    
                    foreach (var column in blockValue.ContentData)
                    {
                        foreach (var columnProperty in column.RawPropertyValues)
                        {
                            // this contains other blocks
                            var block = JsonConvert.DeserializeObject<BlockValue>(columnProperty.Value.ToString());
    
                            if(block == null)
                                continue;
    
                            foreach (var blockData in block.ContentData)
                            {
                                foreach (var blockProperty in blockData.RawPropertyValues)
                                {
                                    if (blockProperty.Key == "body")
                                    {
    
                                        blockData.RawPropertyValues["body"] = new HtmlInputCleaner().CleanUp(blockProperty.Value.ToString());
                                    }
                                }
                            }
    
                            column.RawPropertyValues[columnProperty.Key] = JsonConvert.SerializeObject(block);
    
                        }
                    }
    
                    property.SetValue(JsonConvert.SerializeObject(blockValue));
    
                }
            }
    
        }
    }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft