Is there a way to have the builtin Date Picker with time component default to current datetime?
Title kinda says it all, currently if I have a date time picker on one of my content templates you have to manually select the date and time you want but I'd like it to default to DateTime.Now. This would be especially handy for things like making blog posts where you want to timestamp em on save/publish.
*Note: I'm using uSkinned Site Builder for my site
That is possible! You're going to need to subscribe to INotificationHandler<SendingContentNotification>! It could look something like this:
using System;
using System.Linq;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Models.ContentEditing;
using Umbraco.Cms.Core.Notifications;
using Umbraco.Extensions;
namespace Umbraco.Docs.Samples.Web.Notifications
{
public class EditorSendingContentNotificationHandler : INotificationHandler<SendingContentNotification>
{
public void Handle(SendingContentNotification notification)
{
if (notification.Content.ContentTypeAlias.Equals("blogpost"))
{
// Access the property you want to pre-populate
// each content item can have 'variations' - each variation is represented by the `ContentVariantDisplay` class.
// if your site uses variants, then you need to decide whether to set the default value for all variants or a specific variant
// eg. set by variant name:
// var variant = notification.Content.Variants.FirstOrDefault(f => f.Name == "specificVariantName");
// OR loop through all the variants:
foreach (var variant in notification.Content.Variants)
{
// Check if variant is a 'new variant'
// we only want to set the default value when the content item is first created
if (variant.State == ContentSavedState.NotCreated)
{
// each variant has an IEnumerable of 'Tabs' (property groupings)
// and each of these contain an IEnumerable of `ContentPropertyDisplay` properties
// find the first property with alias 'publishDate'
var pubDateProperty = variant.Tabs.SelectMany(f => f.Properties)
.FirstOrDefault(f => f.Alias.InvariantEquals("publishDate"));
if (pubDateProperty is not null)
{
// set default value of the publish date property if it exists
pubDateProperty.Value = DateTime.UtcNow;
}
}
}
}
}
}
}
Is there a way to have the builtin Date Picker with time component default to current datetime?
Title kinda says it all, currently if I have a date time picker on one of my content templates you have to manually select the date and time you want but I'd like it to default to DateTime.Now. This would be especially handy for things like making blog posts where you want to timestamp em on save/publish.
*Note: I'm using uSkinned Site Builder for my site
Thanks!
Hi!
That is possible! You're going to need to subscribe to
INotificationHandler<SendingContentNotification>
! It could look something like this:(I copy pasted this code from the documentation! :P ) You can read more about it here: https://docs.umbraco.com/umbraco-cms/reference/notifications/editormodel-notifications
//Johannes
is working on a reply...