Copied to clipboard

Flag this post as spam?

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


  • Gordon Saxby 1461 posts 1883 karma points
    Aug 01, 2024 @ 11:22
    Gordon Saxby
    0

    Do not show default message when using CancelOperation

    Umbraco 13

    I am sending my own message when I need to cancel a Sort operation. However, I am still getting the default Umbraco message.

    Is it possible to only show my message?

    enter image description here

    Can I stop the "Failed to Sort Content" message at the top and/or the "Content sorting failed..." message at the bottom - just leaving my "you cannot move the Archive folder" message?

    The code I currently have is:

    public class ContentSortingHandler : INotificationAsyncHandler<ContentSortingNotification>
    {
        public async Task HandleAsync(ContentSortingNotification notification, CancellationToken cancellationToken)
        {
            var archiveFolder = notification.SortedEntities.Count(e => e.ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias);
    
            if (archiveFolder > 0)
            {
                ValidateArticleChildrenSort(notification);
            }
        }
    
        private void ValidateArticleChildrenSort(ContentSortingNotification notification)
        {
            // Find the Archive folder
            var archiveFolderIsLast = notification.SortedEntities.Last().ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias;
    
            if (!archiveFolderIsLast)
                notification.CancelOperation(new EventMessage("Error", "You cannot move the Archive Folder", EventMessageType.Error));
        }
    }
    
  • girish 14 posts 87 karma points
    Aug 02, 2024 @ 07:10
    girish
    0

    To stop the default "Failed to Sort Content" message from appearing and only show your custom message in Umbraco 13, you need to tweak the way notifications are handled.

    Step-by-Step Solution Suppress the Default Message: We need to ensure that only your custom message is displayed, which might involve some customization in the error handling process.

    Custom Error Handling: Override the default behavior to make sure that the system only shows your specific message.

    Here's how you can adjust your code:

    Your Current Code

    public class ContentSortingHandler : INotificationAsyncHandler<ContentSortingNotification>
    {
        public async Task HandleAsync(ContentSortingNotification notification, CancellationToken cancellationToken)
        {
            var archiveFolder = notification.SortedEntities.Count(e => e.ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias);
    
            if (archiveFolder > 0)
            {
                ValidateArticleChildrenSort(notification);
            }
        }
    
        private void ValidateArticleChildrenSort(ContentSortingNotification notification)
        {
            // Find the Archive folder
            var archiveFolderIsLast = notification.SortedEntities.Last().ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias;
    
            if (!archiveFolderIsLast)
                notification.CancelOperation(new EventMessage("Error", "You cannot move the Archive Folder", EventMessageType.Error));
        }
    }
    

    Revised Code

    public class ContentSortingHandler : INotificationAsyncHandler<ContentSortingNotification>
    {
        public async Task HandleAsync(ContentSortingNotification notification, CancellationToken cancellationToken)
        {
            var archiveFolder = notification.SortedEntities.Count(e => e.ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias);
    
            if (archiveFolder > 0)
            {
                ValidateArticleChildrenSort(notification);
            }
        }
    
        private void ValidateArticleChildrenSort(ContentSortingNotification notification)
        {
            // Find the Archive folder
            var archiveFolderIsLast = notification.SortedEntities.Last().ContentType.Alias == ManualArticleArchiveFolder.ModelTypeAlias;
    
            if (!archiveFolderIsLast)
            {
                // Create a custom message and cancel the operation
                var customMessage = new EventMessage("Error", "You cannot move the Archive Folder", EventMessageType.Error);
    
                // Add your custom message
                notification.Messages.Add(customMessage);
    
                // Cancel the operation
                notification.CancelOperation(customMessage);
    
                // Clear default messages to ensure only your message is shown
                notification.Messages.Clear();
                notification.Messages.Add(customMessage);
            }
        }
    }
    

    What We Did:

    Added Your Custom Message: Directly added your custom message to the notification.

    Cleared Default Messages: Cleared any default messages to ensure that only your custom message appears.

    By making these adjustments, when you cancel the sort operation, your custom message "You cannot move the Archive Folder" should be the only message displayed.

  • Gordon Saxby 1461 posts 1883 karma points
    Aug 02, 2024 @ 08:21
    Gordon Saxby
    0

    I get an error saying "Clear()" does not exist?

Please Sign in or register to post replies

Write your reply to:

Draft