Copied to clipboard

Flag this post as spam?

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


  • Brendan Rice 538 posts 1102 karma points
    Apr 15, 2024 @ 17:49
    Brendan Rice
    0

    Clone/copy BlockList from one Node to another.

    I have a DocType/Node with a BlockList that is acting as a template. When a new page is created I want to use that Node and copy a BlockList property on it to a property on the new Node/Page.

    To complicate thing the Parent BlockList has Child BlockLists.

    This is what I have so far. But it doesn't copy child BlockItems that are BlockLists or change UIDs.

    Can anyone help with the clone BlockList please?

    public class MyContentSendingContentNotificationHandler : INotificationHandler

    public MyContentSendingContentNotificationHandler(IContentService contentService, IPublishedContentQuery publishedContentQuery)
    {
        _contentService = contentService;
        _publishedContentQuery = publishedContentQuery;
    }
    
    public void Handle(SendingContentNotification notification)
    {
        var content = notification.Content as ContentItemDisplay;
    
        if (content == null)
            return;
    
        if (content.ContentTypeAlias == "myContent")
        {
            var settingsContent = _contentService.GetById(GetSettingsContentId());
    
            if (settingsContent != null)
            {
                var masterCoursePublished = _publishedContentQuery.ContentAtRoot().FirstOrDefault(x => x.ContentType.Alias == "settings")?.DescendantOfType("myContentMaster");
    
                if (masterCoursePublished != null)
                {
                    var masterCourse = _contentService.GetById(masterCoursePublished.Id);
    
                    if (masterCourse != null)
                    {
    
                        foreach (var variant in notification.Content.Variants)
                        {
                            // Check if variant is a 'new variant'
                            // we only want to set the default value when the content item is first created
                            if (variant.State == ContentSavedState.NotCreated)
                            {
                                // each variant has an IEnumerable of 'Tabs' (property groupings)
                                // and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
                                // find the first property with alias 'publishDate'
                                var blockList = variant.Tabs.SelectMany(f => f.Properties)
                                    .FirstOrDefault(f => f.Alias.InvariantEquals("content"));
                                if (blockList is not null)
                                {
                                    var masterBlockList = masterCourse.GetValue<string>("content");
    
                                    BlockValue blockValue = JsonConvert.DeserializeObject<BlockValue>(masterBlockList);
    
                                    // set default value of the publish date property if it exists
                                    blockList.Value = masterBlockList;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    
    private int GetSettingsContentId()
    {
        return 1071; 
    }
    

    }

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies