diff --git a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs index 24d06542a0..655dfc973d 100644 --- a/src/Umbraco.Infrastructure/Services/Implement/FileService.cs +++ b/src/Umbraco.Infrastructure/Services/Implement/FileService.cs @@ -346,7 +346,7 @@ namespace Umbraco.Cms.Core.Services.Implement using (IScope scope = ScopeProvider.CreateScope()) { - var savingEvent = new TemplateSavingNotification(template, eventMessages, additionalData); + var savingEvent = new TemplateSavingNotification(template, eventMessages, true, contentTypeAlias); if (scope.Notifications.PublishCancelable(savingEvent)) { scope.Complete(); @@ -354,7 +354,7 @@ namespace Umbraco.Cms.Core.Services.Implement } _templateRepository.Save(template); - scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages, savingEvent.AdditionalData).WithStateFrom(savingEvent)); + scope.Notifications.Publish(new TemplateSavedNotification(template, eventMessages).WithStateFrom(savingEvent)); Audit(AuditType.Save, userId, template.Id, ObjectTypes.GetName(UmbracoObjectTypes.Template)); scope.Complete(); diff --git a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs index 5bb971089f..287ad75bb6 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs +++ b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavedNotification.cs @@ -17,12 +17,24 @@ namespace Umbraco.Cms.Infrastructure.Services.Notifications { } - public TemplateSavedNotification(ITemplate target, EventMessages messages, - Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + public bool? CreateTemplateForContentType + { + get + { + State.TryGetValue("CreateTemplateForContentType", out var result); + return result as bool?; + } + set => State["CreateTemplateForContentType"] = value; + } - public TemplateSavedNotification(IEnumerable target, EventMessages messages, - Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; - - public Dictionary AdditionalData; + public string ContentTypeAlias + { + get + { + State.TryGetValue("ContentTypeAlias", out var result); + return result as string; + } + set => State["ContentTypeAlias"] = value; + } } } diff --git a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs index fc12eda6c3..345d9ecf20 100644 --- a/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs +++ b/src/Umbraco.Infrastructure/Services/Notifications/TemplateSavingNotification.cs @@ -17,12 +17,39 @@ namespace Umbraco.Cms.Infrastructure.Services.Notifications { } - public TemplateSavingNotification(ITemplate target, EventMessages messages, - Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + public TemplateSavingNotification(ITemplate target, EventMessages messages, bool createTemplateForContentType, + string contentTypeAlias) : base(target, messages) + { + CreateTemplateForContentType = createTemplateForContentType; + ContentTypeAlias = contentTypeAlias; + } public TemplateSavingNotification(IEnumerable target, EventMessages messages, - Dictionary additionalData) : base(target, messages) => AdditionalData = additionalData; + bool createTemplateForContentType, + string contentTypeAlias) : base(target, messages) + { + CreateTemplateForContentType = createTemplateForContentType; + ContentTypeAlias = contentTypeAlias; + } - public Dictionary AdditionalData; + public bool? CreateTemplateForContentType + { + get + { + State.TryGetValue("CreateTemplateForContentType", out var result); + return result as bool?; + } + set => State["CreateTemplateForContentType"] = value; + } + + public string ContentTypeAlias + { + get + { + State.TryGetValue("ContentTypeAlias", out var result); + return result as string; + } + set => State["ContentTypeAlias"] = value; + } } } diff --git a/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs b/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs index 682b854dcb..66c3013596 100644 --- a/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs +++ b/src/Umbraco.Web.Common/ModelsBuilder/ModelsBuilderNotificationHandler.cs @@ -97,16 +97,10 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder return; } - // don't do anything if this special key is not found - if (notification.AdditionalData is null || !notification.AdditionalData.ContainsKey("CreateTemplateForContentType")) - { - return; - } - // ensure we have the content type alias - if (!notification.AdditionalData.ContainsKey("ContentTypeAlias")) + if (notification.ContentTypeAlias is null) { - throw new InvalidOperationException("The additionalData key: ContentTypeAlias was not found"); + throw new InvalidOperationException("ContentTypeAlias was not found on the notification"); } foreach (ITemplate template in notification.SavedEntities) @@ -117,7 +111,7 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder { // ensure is safe and always pascal cased, per razor standard // + this is how we get the default model name in Umbraco.ModelsBuilder.Umbraco.Application - var alias = notification.AdditionalData["ContentTypeAlias"].ToString(); + var alias = notification.ContentTypeAlias; var name = template.Name; // will be the name of the content type since we are creating var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);