Copied to clipboard

Flag this post as spam?

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


  • Ahmed Mostafa 3 posts 83 karma points
    Aug 07, 2022 @ 07:31
    Ahmed Mostafa
    0

    Unpublishing content triggers publishing handler before unpublishing handler unless user select to unpublish all languages

    Hello All,

    We are using Umbraco 9.5 (.Net core 5) and we need to do some validations while unpublishing some contents, so we have implemented the class:

    public class ContentService_UnPublishingHandler : INotificationHandler<ContentUnpublishingNotification>
    {
        private readonly CommonValidationHelper commonValidationHelper;
        private ContentCacheHelper contentCacheHelper;
    
        public ContentService_UnPublishingHandler(CommonValidationHelper commonValidationHelper, ContentCacheHelper contentCacheHelper)
        {
            this.commonValidationHelper = commonValidationHelper;
            this.contentCacheHelper = contentCacheHelper;
        }
        public void Handle(ContentUnpublishingNotification notification)
        {
            ...
        }
    }
    

    but we found the event of Publishing handler always invoke before Unpublishing handler if we accomplish to unpublish content for only one language, but in case of unpublishing for all language then Unpublishing handler only invoked.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Aug 07, 2022 @ 09:30
    Marc Goodson
    100

    HI Ahmed

    It's not immediately intuitive, but this is 'by design' and largely due to how Umbraco has evolved over the years to introduce language variants.

    The notifications relate to the Underlying Content Item - and the language version, is just a variation of that Content Item, so when a language variation is 'UnPublished', the underlying Content item isn't necessarily 'UnPublished' - but it needs to be 'Saved' and the PublishedCache needs to be updated to remove the language variations.

    Since people might write code to handle whenever a Content Item is Published/Updated, then the Published notification needs to fire in this instance!

    If all language variations are 'UnPublished' then the underlying Content Item IS also UnPublished, and the UnPublish Notification is triggered.

    There is a slightly better explanation here: https://github.com/umbraco/Umbraco-CMS/issues/5410#issuecomment-736909457

    There is on Notifications several flags to provide context to what might have triggered the Publish and UnPublish notification, there is one called:

    HasUnpublishedCulture
    

    and another called:

    IsUnpublishingCulture 
    

    https://github.com/umbraco/Umbraco-CMS/blob/e626fca2432582f052cb13654eedd9e60ef8723f/src/Umbraco.Core/Notifications/ContentNotificationExtensions.cs#L51

    That you can use when detecting an 'Unpublish' of a Language version, in a 'Published' or 'Publishing' Notification!

    regards

    marc

Please Sign in or register to post replies

Write your reply to:

Draft