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?
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").
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";
}
}
}
}
}
}
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?
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.
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
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").
So I ended up with this code:
is working on a reply...