Copied to clipboard

Flag this post as spam?

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


  • Martin Rud 261 posts 1002 karma points c-trib
    May 22, 2021 @ 12:54
    Martin Rud
    0

    Automatically name new nodes with content from datetime picker

    I have a document type that represents a time span. It is created as child nodes to an event node.

    Right now editors have to name these time span nodes manually, but its feels like a tedious and unnecessary task for them since the only reasonable name is the time(s) that the node represents.

    Can some kind of (JavaScript) be made that inserts the startTime property value in the node name field?

    enter image description here enter image description here

  • Søren Kottal 712 posts 4570 karma points MVP 6x c-trib
    May 22, 2021 @ 13:01
    Søren Kottal
    1

    You can use the EditorModelEventManager to prefill the name, when a new node gets created.

    https://our.umbraco.com/Documentation/Reference/Events/EditorModel-Events/

    Then you can maybe also add a Saving event handler, to change the name to whatever is selected in your date pickers.

  • David Armitage 509 posts 2079 karma points
    May 22, 2021 @ 13:29
    David Armitage
    100

    Hi Martin,

    Consider using Umbraco's publishing event. I have done the exact same thing a few times.

    The only issue is you will have to out any random name in. Capture it in the publishing event and rename on the fly.

    Like I said I have done the same. Usually appending information to the from of the name but the same concept will work for renaming it 100%.

    Hope this helps.

    Regards

    David

  • Martin Rud 261 posts 1002 karma points c-trib
    May 22, 2021 @ 14:02
    Martin Rud
    0

    Cool! I ended up deciding that the node name hadn´t got to be descriptive since instead I just show the datetime property in the list view ("Tidsrum" is Danish for "Time span").

    enter image description here

    So I ended up with this code:

    using System;
    using System.Linq;
    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Models.ContentEditing;
    
    namespace My.Website
    {
        [RuntimeLevel(MinLevel = RuntimeLevel.Run)]
        public class SubscribeToEditorModelEventsComposer : ComponentComposer<SubscribeToEditorModelEvents>
        {
        //this automatically adds the component to the Components collection of the Umbraco composition
        }
    
        public class SubscribeToEditorModelEvents : IComponent
        {
            // initialize: runs once when Umbraco starts
            public void Initialize()
            {
                EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
            }
    
            // terminate: runs once when Umbraco stops
            public void Terminate()
            {
                //unsubscribe during shutdown
                EditorModelEventManager.SendingContentModel -= EditorModelEventManager_SendingContentModel;
            }
    
            private void EditorModelEventManager_SendingContentModel(System.Web.Http.Filters.HttpActionExecutedContext sender, EditorModelEventArgs<Umbraco.Web.Models.ContentEditing.ContentItemDisplay> e)
            {
    
                foreach (var variant in e.Model.Variants)
                {
                    if (variant.State == ContentSavedState.NotCreated)
                    {
    
    
                        if (e.Model.ContentTypeAlias == "SiteEventTimespan") {
                            variant.Name = "Time span";
                        }
                    }
    
                }
    
            }
        }
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft