Copied to clipboard

Flag this post as spam?

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


  • Lyle Phillips 3 posts 73 karma points
    Mar 03, 2024 @ 18:16
    Lyle Phillips
    0

    Dynamically change name of content item on creation in back office.

    Hi Guys, im new to Umbraco since 2024 and we are using version 13.

    I need to hook into some kind of event handler when content is created. I've found some examples for previous versions of Umbraco but the same namespaces/classes do not exist in v13.

    Basically I want to change the name of the content item dynamically when created in the back office. I want to add a property value to the name of the content item.

    Example I have a doc type with a property called "group".

    I want the name of the name of the content item created to include the "group" value that's inputted by the user.

    I hope that clear and I welcome myself to this community.

    Thanks all

  • Marc Goodson 2157 posts 14431 karma points MVP 9x c-trib
    Mar 04, 2024 @ 21:14
    Marc Goodson
    1

    Hi Lyle

    Notifications! Is the new name for events!!

    https://docs.umbraco.com/umbraco-cms/reference/notifications

    There are two notifications that get broadcast that might be handy for what you are trying to achieve....

    1) firstly the SendingContentNotification

    https://docs.umbraco.com/umbraco-cms/reference/notifications/editormodel-notifications

    This is broadcast 'just before' the content is loaded in the backoffice ready to be edited... ... This works well if you want to suggest a convention, repopulate a field, set article date to be today's date... Things like that, but ultimately the editor can choose to change your defaults and save and publish what they like..

    2) secondly, the Content Saving Notification

    https://docs.umbraco.com/umbraco-cms/reference/notifications/contentservice-notifications

    This notification is broadcast when the editor presses save and publish on a page they have edited...

    You have access to what they have entered for each field and can write code to check and augment these values before it is saved and published... Here your code can cancel the saving event and send a message temporarily back to the editor... Or can just add:update values eg, set an article date to be today's date if it has been left blank or read the parent group node and prep end it to the name of the node being saved!

    Anyway hopefully that's enough of a steer to get you on the right track!

    Regards

    Marc

  • Lyle Phillips 3 posts 73 karma points
    Mar 05, 2024 @ 09:53
    Lyle Phillips
    0

    Thanks Marc, I managed to implement the ContentSavingNotification.

    using Umbraco.Cms.Core.Events;
    using Umbraco.Cms.Core.Notifications;
    
    namespace UmbracoProject.Custom
    {
        public class ContentSaving: INotificationHandler<ContentSavingNotification>
        {
            private string TagProperty = "tagProperty";
            private string TagGroup = "tagGroup";
            private string TagName = "tagName";
            public void Handle(ContentSavingNotification notification)
            {
                foreach (var contentItem in notification.SavedEntities)
                {
                    if (contentItem.ContentType.Alias == TagProperty)
                    {
                        var tagGroup = contentItem.GetValue<string>(TagGroup);
                        var tagName = contentItem.GetValue<string>(TagName);
    
                        if (tagGroup != null && tagName != null)
                        {
                            contentItem.Name = $"{tagGroup}: {tagName}";
                        }
                    }
                }
            }
        }
    }
    
  • Lyle Phillips 3 posts 73 karma points
    Mar 05, 2024 @ 14:25
    Lyle Phillips
    0

    Hi again Marc,

    I get this error a lot because of trying to create nested content document types. Can you shed some light on how to fix this issue in version 13. See below.

    Your help would be really appreciated.

    enter image description here

Please Sign in or register to post replies

Write your reply to:

Draft