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?
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));
}
}
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.
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?
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:
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
Revised Code
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.
I get an error saying "Clear()" does not exist?
is working on a reply...