From e911a33cf3f619ac3ef6247b7df25fc8d5a8c3a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5kan=20Edling?= Date: Tue, 5 May 2020 09:19:15 +0200 Subject: [PATCH] Added manager init for fields. Fixes #1144 --- .../Services/PageService.cs | 4 + .../Services/PostService.cs | 4 + .../Services/SiteService.cs | 4 + core/Piranha/Services/ContentFactory.cs | 114 ++++++++++++++---- core/Piranha/Services/IContentFactory.cs | 18 +++ 5 files changed, 119 insertions(+), 25 deletions(-) diff --git a/core/Piranha.Manager.Core/Services/PageService.cs b/core/Piranha.Manager.Core/Services/PageService.cs index 27e1ab303..1bf4727ac 100644 --- a/core/Piranha.Manager.Core/Services/PageService.cs +++ b/core/Piranha.Manager.Core/Services/PageService.cs @@ -208,6 +208,10 @@ public async Task GetById(Guid id, bool useDraft = true) if (page != null) { + // Perform manager init + await _factory.InitDynamicManagerAsync(page, + App.PageTypes.GetById(page.TypeId)); + var model = Transform(page, isDraft); model.PendingCommentCount = (await _api.Pages.GetAllPendingCommentsAsync(id)) diff --git a/core/Piranha.Manager.Core/Services/PostService.cs b/core/Piranha.Manager.Core/Services/PostService.cs index 6400c53f4..7ef53c44f 100644 --- a/core/Piranha.Manager.Core/Services/PostService.cs +++ b/core/Piranha.Manager.Core/Services/PostService.cs @@ -194,6 +194,10 @@ public async Task GetById(Guid id, bool useDraft = true) if (post != null) { + // Perform manager init + await _factory.InitDynamicManagerAsync(post, + App.PostTypes.GetById(post.TypeId)); + var postModel = Transform(post, isDraft); postModel.Categories = (await _api.Posts.GetAllCategoriesAsync(post.BlogId)) diff --git a/core/Piranha.Manager.Core/Services/SiteService.cs b/core/Piranha.Manager.Core/Services/SiteService.cs index 3e4772926..b8f31c5a8 100644 --- a/core/Piranha.Manager.Core/Services/SiteService.cs +++ b/core/Piranha.Manager.Core/Services/SiteService.cs @@ -65,6 +65,10 @@ public async Task GetContentById(Guid id) if (site != null) { + // Perform manager init + await _factory.InitDynamicManagerAsync(site, + App.SiteTypes.GetById(site.TypeId)); + return Transform(site); } return null; diff --git a/core/Piranha/Services/ContentFactory.cs b/core/Piranha/Services/ContentFactory.cs index 26782cb01..3ec4f111a 100644 --- a/core/Piranha/Services/ContentFactory.cs +++ b/core/Piranha/Services/ContentFactory.cs @@ -13,6 +13,7 @@ using System.Collections.Generic; using System.Dynamic; using System.Linq; +using System.Reflection; using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Piranha.Extend; @@ -93,7 +94,7 @@ public async Task CreateBlockAsync(string typeName) if (typeof(Extend.IField).IsAssignableFrom(prop.PropertyType)) { var field = Activator.CreateInstance(prop.PropertyType); - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, false).ConfigureAwait(false); prop.SetValue(block, field); } } @@ -213,7 +214,32 @@ private async Task CreateModelAsync(ContentTypeBase type) where T : Conten /// The content type /// The model type /// The initialized model - public async Task InitDynamicAsync(T model, ContentTypeBase type) where T : IDynamicContent + public Task InitDynamicAsync(T model, ContentTypeBase type) where T : IDynamicContent + { + return InitDynamicAsync(model, type, false); + } + + /// + /// Initializes the given dynamic model for the manager. + /// + /// The model + /// The content type + /// The model type + /// The initialized model + public Task InitDynamicManagerAsync(T model, ContentTypeBase type) where T : IDynamicContent + { + return InitDynamicAsync(model, type, true); + } + + /// + /// Initializes the given dynamic model. + /// + /// The model + /// The content type + /// If this is initialization used by the manager + /// The model type + /// The initialized model + private async Task InitDynamicAsync(T model, ContentTypeBase type, bool managerInit) where T : IDynamicContent { using (var scope = _services.CreateScope()) { @@ -225,14 +251,14 @@ public async Task InitDynamicAsync(T model, ContentTypeBase type) where T if (!regionType.Collection) { // Initialize it - await InitDynamicRegionAsync(scope, region, regionType).ConfigureAwait(false); + await InitDynamicRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false); } else { // This region was a collection. Initialize all items foreach (var item in (IList)region) { - await InitDynamicRegionAsync(scope, item, regionType).ConfigureAwait(false); + await InitDynamicRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false); } } } @@ -242,13 +268,13 @@ public async Task InitDynamicAsync(T model, ContentTypeBase type) where T { foreach (var block in blockModel.Blocks) { - await InitBlockAsync(scope, block).ConfigureAwait(false); + await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false); if (block is BlockGroup blockGroup) { foreach (var child in blockGroup.Items) { - await InitBlockAsync(scope, child).ConfigureAwait(false); + await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false); } } } @@ -264,7 +290,32 @@ public async Task InitDynamicAsync(T model, ContentTypeBase type) where T /// The content type /// The model type /// The initialized model - public async Task InitAsync(T model, ContentTypeBase type) where T : ContentBase + public Task InitAsync(T model, ContentTypeBase type) where T : ContentBase + { + return InitAsync(model, type, false); + } + + /// + /// Initializes the given model for the manager. + /// + /// The model + /// The content type + /// The model type + /// The initialized model + public Task InitManagerAsync(T model, ContentTypeBase type) where T : ContentBase + { + return InitAsync(model, type, true); + } + + /// + /// Initializes the given model. + /// + /// The model + /// The content type + /// If this is initialization used by the manager + /// The model type + /// The initialized model + private async Task InitAsync(T model, ContentTypeBase type, bool managerInit) where T : ContentBase { if (model is IDynamicContent) { @@ -283,14 +334,14 @@ public async Task InitAsync(T model, ContentTypeBase type) where T : Conte if (!regionType.Collection) { // Initialize it - await InitRegionAsync(scope, region, regionType).ConfigureAwait(false); + await InitRegionAsync(scope, region, regionType, managerInit).ConfigureAwait(false); } else { // This region was a collection. Initialize all items foreach (var item in (IList)region) { - await InitRegionAsync(scope, item, regionType).ConfigureAwait(false); + await InitRegionAsync(scope, item, regionType, managerInit).ConfigureAwait(false); } } } @@ -300,13 +351,13 @@ public async Task InitAsync(T model, ContentTypeBase type) where T : Conte { foreach (var block in blockModel.Blocks) { - await InitBlockAsync(scope, block).ConfigureAwait(false); + await InitBlockAsync(scope, block, managerInit).ConfigureAwait(false); if (block is Extend.BlockGroup) { foreach (var child in ((Extend.BlockGroup)block).Items) { - await InitBlockAsync(scope, child).ConfigureAwait(false); + await InitBlockAsync(scope, child, managerInit).ConfigureAwait(false); } } } @@ -321,7 +372,8 @@ public async Task InitAsync(T model, ContentTypeBase type) where T : Conte /// The current service scope /// The region /// The region type - private async Task InitDynamicRegionAsync(IServiceScope scope, object region, RegionType regionType) + /// If this is initialization used by the manager + private async Task InitDynamicRegionAsync(IServiceScope scope, object region, RegionType regionType, bool managerInit) { if (region != null) { @@ -329,7 +381,7 @@ private async Task InitDynamicRegionAsync(IServiceScope scope, object region, Re { // This region only has one field, that means // the region is in fact a field. - await InitFieldAsync(scope, region).ConfigureAwait(false); + await InitFieldAsync(scope, region, managerInit).ConfigureAwait(false); } else { @@ -338,7 +390,7 @@ private async Task InitDynamicRegionAsync(IServiceScope scope, object region, Re { if (((IDictionary)region).TryGetValue(fieldType.Id, out var field)) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false); } } } @@ -351,7 +403,8 @@ private async Task InitDynamicRegionAsync(IServiceScope scope, object region, Re /// The current service scope /// The region /// The region type - private async Task InitRegionAsync(IServiceScope scope, object region, RegionType regionType) + /// If this is initialization used by the manager + private async Task InitRegionAsync(IServiceScope scope, object region, RegionType regionType, bool managerInit) { if (region != null) { @@ -359,7 +412,7 @@ private async Task InitRegionAsync(IServiceScope scope, object region, RegionTyp { // This region only has one field, that means // the region is in fact a field. - await InitFieldAsync(scope, region).ConfigureAwait(false); + await InitFieldAsync(scope, region, managerInit).ConfigureAwait(false); } else { @@ -372,7 +425,7 @@ private async Task InitRegionAsync(IServiceScope scope, object region, RegionTyp if (field != null) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false); } } } @@ -384,7 +437,8 @@ private async Task InitRegionAsync(IServiceScope scope, object region, RegionTyp /// /// The current service scope /// The block - private async Task InitBlockAsync(IServiceScope scope, Extend.Block block) + /// If this is initialization used by the manager + private async Task InitBlockAsync(IServiceScope scope, Extend.Block block, bool managerInit) { if (block != null) { @@ -399,7 +453,7 @@ private async Task InitBlockAsync(IServiceScope scope, Extend.Block block) if (field != null) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, managerInit).ConfigureAwait(false); } } } @@ -422,7 +476,7 @@ private async Task CreateDynamicRegionAsync(IServiceScope scope, RegionT { if (initFields) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, false).ConfigureAwait(false); } return field; } @@ -438,7 +492,7 @@ private async Task CreateDynamicRegionAsync(IServiceScope scope, RegionT { if (initFields) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, false).ConfigureAwait(false); } ((IDictionary)reg).Add(fieldType.Id, field); } @@ -466,7 +520,7 @@ private async Task CreateRegionAsync(IServiceScope scope, object model, { if (initFields) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, false).ConfigureAwait(false); } return field; } @@ -486,7 +540,7 @@ private async Task CreateRegionAsync(IServiceScope scope, object model, { if (initFields) { - await InitFieldAsync(scope, field).ConfigureAwait(false); + await InitFieldAsync(scope, field, false).ConfigureAwait(false); } reg.GetType().SetPropertyValue(fieldType.Id, reg, field); } @@ -518,10 +572,20 @@ protected object CreateField(FieldType fieldType) /// /// The current service scope /// The field + /// If this is initialization used by the manager /// The initialized field - protected async Task InitFieldAsync(IServiceScope scope, object field) + protected async Task InitFieldAsync(IServiceScope scope, object field, bool managerInit) { - var init = field.GetType().GetMethod("Init"); + MethodInfo init = null; + + if (!managerInit) + { + init = field.GetType().GetMethod("Init"); + } + else + { + init = field.GetType().GetMethod("InitManager"); + } if (init != null) { diff --git a/core/Piranha/Services/IContentFactory.cs b/core/Piranha/Services/IContentFactory.cs index 4b22e445a..250da014b 100644 --- a/core/Piranha/Services/IContentFactory.cs +++ b/core/Piranha/Services/IContentFactory.cs @@ -47,6 +47,15 @@ public interface IContentFactory /// The initialized model Task InitAsync(T model, ContentTypeBase type) where T : ContentBase; + /// + /// Initializes the given model for the manager. + /// + /// The model + /// The content type + /// The model type + /// The initialized model + Task InitManagerAsync(T model, ContentTypeBase type) where T : ContentBase; + /// /// Initializes the given dynamic model. /// @@ -55,5 +64,14 @@ public interface IContentFactory /// The model type /// The initialized model Task InitDynamicAsync(T model, ContentTypeBase type) where T : IDynamicContent; + + /// + /// Initializes the given dynamic model. + /// + /// The model + /// The content type + /// The model type + /// The initialized model + Task InitDynamicManagerAsync(T model, ContentTypeBase type) where T : IDynamicContent; } } \ No newline at end of file