Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Web.Http.Filters;
- using Umbraco.Core;
- using Umbraco.Core.Composing;
- using Umbraco.Core.Logging;
- using Umbraco.Web.Editors;
- using Umbraco.Web.Models.ContentEditing;
- //If using Models Builder generated models from the default namespace
- using Umbraco.Web.PublishedModels;
- public class EditorModelComponent : IComponent
- {
- private readonly ILogger logService;
- public EditorModelComponent(ILogger logService) {
- this.logService = logService;
- }
- public void Initialize() {
- EditorModelEventManager.SendingContentModel += EditorModelEventManager_SendingContentModel;
- }
- public void Terminate() { return; }
- private void EditorModelEventManager_SendingContentModel(HttpActionExecutedContext sender, EditorModelEventArgs<ContentItemDisplay> e) {
- SetDefaultValue(e, new string[] { "news" }, "date", DateTime.Now.Date);
- }
- private void SetDefaultValue(EditorModelEventArgs<ContentItemDisplay> e, string[] docTypeAliases, string propertyAlias, object newValue) {
- try {
- if(docTypeAliases.Contains(e.Model.ContentTypeAlias))
- {
- //Here we need to consider variants
- foreach(var variant in e.Model.Variants)
- {
- var props = variant.Tabs.SelectMany( t =>
- t.Properties.Where(p =>
- p.Alias.Equals(propertyAlias) &&
- (p.Value == null || string.IsNullOrWhiteSpace(p.Value.ToString()))));
- foreach(var prop in props)
- prop.Value = newValue;
- }
- }
- }
- catch (Exception ex)
- {
- logService.Error(GetType(), ex, "Error occurred setting default value");
- }
- }
- }
- public class EditorModelComposer : IUserComposer
- {
- public void Compose(Composition composition) {
- composition.Components().Append<EditorModelComponent>();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement