Use notifications for saving and saved template

This was was a bit more interesting.
This commit is contained in:
Mole
2021-03-30 14:01:37 +02:00
parent 5c4a6b8f6e
commit 52e31435cd
8 changed files with 64 additions and 62 deletions

View File

@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Services.Implement;
using Umbraco.Cms.Core.Strings;
using Umbraco.Cms.Infrastructure.ModelsBuilder;
using Umbraco.Cms.Infrastructure.Services.Notifications;
using Umbraco.Cms.Infrastructure.WebAssets;
using Umbraco.Cms.Web.Common.ModelBinders;
@@ -19,7 +20,10 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
/// <summary>
/// Handles <see cref="UmbracoApplicationStarting"/> and <see cref="ServerVariablesParsing"/> notifications to initialize MB
/// </summary>
internal class ModelsBuilderNotificationHandler : INotificationHandler<UmbracoApplicationStarting>, INotificationHandler<ServerVariablesParsing>, INotificationHandler<ModelBindingError>
internal class ModelsBuilderNotificationHandler :
INotificationHandler<ServerVariablesParsing>,
INotificationHandler<ModelBindingError>,
INotificationHandler<TemplateSavingNotification>
{
private readonly ModelsBuilderSettings _config;
private readonly IShortStringHelper _shortStringHelper;
@@ -35,19 +39,6 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
_modelsBuilderDashboardProvider = modelsBuilderDashboardProvider;
}
/// <summary>
/// Handles the <see cref="UmbracoApplicationStarting"/> notification
/// </summary>
public void Handle(UmbracoApplicationStarting notification)
{
// always setup the dashboard
// note: UmbracoApiController instances are automatically registered
if (_config.ModelsMode != ModelsMode.Nothing)
{
FileService.SavingTemplate += FileService_SavingTemplate;
}
}
/// <summary>
/// Handles the <see cref="ServerVariablesParsing"/> notification to add custom urls and MB mode
/// </summary>
@@ -99,21 +90,26 @@ namespace Umbraco.Cms.Web.Common.ModelsBuilder
/// Used to check if a template is being created based on a document type, in this case we need to
/// ensure the template markup is correct based on the model name of the document type
/// </summary>
private void FileService_SavingTemplate(IFileService sender, SaveEventArgs<ITemplate> e)
public void Handle(TemplateSavingNotification notification)
{
if (_config.ModelsMode == ModelsMode.Nothing)
{
return;
}
// don't do anything if this special key is not found
if (!e.AdditionalData.ContainsKey("CreateTemplateForContentType"))
if (!notification.AdditionalData.ContainsKey("CreateTemplateForContentType"))
{
return;
}
// ensure we have the content type alias
if (!e.AdditionalData.ContainsKey("ContentTypeAlias"))
if (!notification.AdditionalData.ContainsKey("ContentTypeAlias"))
{
throw new InvalidOperationException("The additionalData key: ContentTypeAlias was not found");
}
foreach (ITemplate template in e.SavedEntities)
foreach (ITemplate template in notification.SavedEntities)
{
// if it is in fact a new entity (not been saved yet) and the "CreateTemplateForContentType" key
// is found, then it means a new template is being created based on the creation of a document type
@@ -121,7 +117,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 = e.AdditionalData["ContentTypeAlias"].ToString();
var alias = notification.AdditionalData["ContentTypeAlias"].ToString();
var name = template.Name; // will be the name of the content type since we are creating
var className = UmbracoServices.GetClrName(_shortStringHelper, name, alias);