Copied to clipboard

Flag this post as spam?

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


  • Dominic Resch 45 posts 115 karma points
    Jul 09, 2024 @ 13:01
    Dominic Resch
    0

    Changes during ContentSavingNotification

    Hello everyone,

    I am currently trying to make changes during the "ContentSavingNotification" event.

    This is working so far.

    I have a Multi Node Tree Picker and a Tags Property in my Document Type.

    When I change something in the MNTP, the names of the selected nodes should automatically be saved in the tags property in the correct language.

    This also works. But my problem is, when I select a node in the MNTP, save/publish and then navigate to another page, I get the message that there are unsaved changes. If I discard them and navigate to another page anyway and go back, the changes are apparently all saved.

    If I save/publish the page twice, I don't get the message.

    What could be the reason for this? Do I somehow need to say that the tags property is "dirty" or something?

    Here is my handler (just code for testing): "tags" is my MNTP, umbTags is the actual "Tags" property

    public class MultiLanguageTagHandler(
        ILocalizationService localizationService,
        IUmbracoContextAccessor umbracoContextAccessor) : INotificationHandler<ContentSavingNotification>
    {
        public void Handle(ContentSavingNotification notification)
        {
            foreach (IContent content in notification.SavedEntities)
            {
                foreach (string language in content.AvailableCultures)
                {
                    IUmbracoContext umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
    
                    if (content.HasProperty("tags") && content.GetValue("tags", language) is string nodeIds)
                    {
                        IEnumerable<string?> tags = nodeIds
                            .Split(',')
                            .Select(x => umbracoContext.Content!.GetById(UdiParser.Parse(x))?.Name(language))
                            .Where(x => !string.IsNullOrWhiteSpace(x));
    
                        if (tags.Count() != nodeIds.Split(',').Length)
                        {
                            notification.Messages.Add(new EventMessage("Error while saving",
                                "Some tags have not been published in the given culture.", EventMessageType.Error));
    
                            notification.Cancel = true;
    
                            continue;
                        }
    
                        content.SetValue("umbTags", string.Join(",", tags), language);
                    }
                }
            }
        }
    }
    
  • Dominic Resch 45 posts 115 karma points
    Jul 09, 2024 @ 16:12
    Dominic Resch
    0

    I have now done some more testing: The problem seems to be the "tags" property editor. If I use an MNTP and a text string as property editor instead, then it works without a message that there are unsaved changes.

    Could it be that the tags property editor, after saving, queries the data again to display it and that's why it causes problems?

  • Dominic Resch 45 posts 115 karma points
    Jul 09, 2024 @ 17:31
    Dominic Resch
    0

    If I remove the property via SendingContentNotification, then it works as it should:

    public class HideUmbTagsHandler : INotificationHandler<SendingContentNotification>
    {
        public void Handle(SendingContentNotification notification)
        {
            foreach (ContentVariantDisplay variant in notification.Content.Variants)
            {
                foreach (Tab<ContentPropertyDisplay> tab in variant.Tabs)
                {
                    tab.Properties = tab.Properties?.Where(x => x.Alias != "umbTags");
                }
            }
        }
    }
    

    That's fine for me for now, since I wanted to remove/hide the property anyway. However, users of certain groups should still see the property (read-only), if only for "debugging" reasons. So it would be nice if someone could help me here.

Please Sign in or register to post replies

Write your reply to:

Draft