From cdfb477c1da2728be8fbd5ffe570867731ccbfb7 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 10 Oct 2023 06:08:28 -0700 Subject: [PATCH 1/3] Fix an exception is ListPart with header Fix #14472 --- .../Drivers/ContainedPartDisplayDriver.cs | 17 +++++++---------- .../Views/ListPartHeaderAdmin.cshtml | 5 +++++ 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs index a558e714da8..043d6ec9b7a 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -127,7 +126,7 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten results.Add(Initialize("ListPartNavigationAdmin", async model => { model.ContainedContentTypeDefinitions = GetContainedContentTypes(settings).ToArray(); - model.Container = await _contentManager.GetAsync(containerId, VersionOptions.Latest); + model.Container = await GetContainerAsync(containerId); model.EnableOrdering = settings.EnableOrdering; model.ContainerContentTypeDefinition = definition; }).Location("Content:1.5")); @@ -147,14 +146,7 @@ private IDisplayResult GetListPartHeader(string containerId, ListPartSettings li { return Initialize("ListPartHeaderAdmin", async model => { - var container = await _contentManager.GetAsync(containerId); - - if (container == null) - { - return; - } - - model.ContainerContentItem = container; + model.ContainerContentItem = await GetContainerAsync(containerId); if (listPartSettings != null) { @@ -164,6 +156,11 @@ private IDisplayResult GetListPartHeader(string containerId, ListPartSettings li }).Location("Content:1"); } + // Initially, attempt to locate a published container. + // If none is found, try acquiring the most recent unpublished version. + private async Task GetContainerAsync(string containerId) + => await _contentManager.GetAsync(containerId) ?? await _contentManager.GetAsync(containerId, VersionOptions.Latest); + private IEnumerable GetContainedContentTypes(ListPartSettings settings) => settings.ContainedContentTypes ?.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType)) diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml b/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml index 4d1e43c6f80..91c2fe9f285 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml @@ -2,6 +2,11 @@ @inject OrchardCore.ContentManagement.Display.IContentItemDisplayManager ContentItemDisplayManager @inject OrchardCore.DisplayManagement.ModelBinding.IUpdateModelAccessor UpdateModelAccessor +@if (Model.ContainerContentItem == null) +{ + return; +} +
@await DisplayAsync(await ContentItemDisplayManager.BuildDisplayAsync(Model.ContainerContentItem, UpdateModelAccessor.ModelUpdater, "HeaderAdmin")) From 45036dc1296815c7c0dc7983f1ca6a168c9ebfe2 Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 10 Oct 2023 06:22:47 -0700 Subject: [PATCH 2/3] Minor cleanup --- .../Drivers/ContainedPartDisplayDriver.cs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs index 043d6ec9b7a..ad182587e9b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs @@ -122,7 +122,7 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten if (settings != null) { - // Add list part navigation + // Add list part navigation. results.Add(Initialize("ListPartNavigationAdmin", async model => { model.ContainedContentTypeDefinitions = GetContainedContentTypes(settings).ToArray(); @@ -143,8 +143,7 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten } private IDisplayResult GetListPartHeader(string containerId, ListPartSettings listPartSettings) - { - return Initialize("ListPartHeaderAdmin", async model => + => Initialize("ListPartHeaderAdmin", async model => { model.ContainerContentItem = await GetContainerAsync(containerId); @@ -154,17 +153,16 @@ private IDisplayResult GetListPartHeader(string containerId, ListPartSettings li model.EnableOrdering = listPartSettings.EnableOrdering; } }).Location("Content:1"); - } // Initially, attempt to locate a published container. // If none is found, try acquiring the most recent unpublished version. private async Task GetContainerAsync(string containerId) => await _contentManager.GetAsync(containerId) ?? await _contentManager.GetAsync(containerId, VersionOptions.Latest); - private IEnumerable GetContainedContentTypes(ListPartSettings settings) => - settings.ContainedContentTypes - ?.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType)) - .Where(definition => definition is not null) - ?? Enumerable.Empty(); + private IEnumerable GetContainedContentTypes(ListPartSettings settings) + => settings.ContainedContentTypes + ?.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType)) + .Where(definition => definition is not null) + ?? Enumerable.Empty(); } } From f3ebf877d56b2b9a373b3dad3b14486c796a180d Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Tue, 10 Oct 2023 09:42:16 -0700 Subject: [PATCH 3/3] Cleanup --- .../Drivers/ContainedPartDisplayDriver.cs | 37 +++++++++++-------- .../Views/ListPartHeaderAdmin.cshtml | 5 --- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs index ad182587e9b..b215dda5442 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Drivers/ContainedPartDisplayDriver.cs @@ -40,7 +40,7 @@ public override async Task EditAsync(ContentItem model, IUpdateM if (containedPart != null) { - return BuildViewModel(containedPart.ListContentItemId, containedPart.ListContentType, model.ContentType); + return await BuildViewModelAsync(containedPart.ListContentItemId, containedPart.ListContentType, model.ContentType); } var viewModel = new EditContainedPartViewModel(); @@ -65,7 +65,7 @@ await model.AlterAsync(async part => } }); - return BuildViewModel(viewModel.ContainerId, viewModel.ContainerContentType, model.ContentType, viewModel.EnableOrdering); + return await BuildViewModelAsync(viewModel.ContainerId, viewModel.ContainerContentType, model.ContentType, viewModel.EnableOrdering); } return null; @@ -97,7 +97,7 @@ await model.AlterAsync(async part => return await EditAsync(model, updater); } - private IDisplayResult BuildViewModel(string containerId, string containerContentType, string contentType, bool enableOrdering = false) + private async Task BuildViewModelAsync(string containerId, string containerContentType, string contentType, bool enableOrdering = false) { var results = new List() { @@ -122,18 +122,23 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten if (settings != null) { - // Add list part navigation. - results.Add(Initialize("ListPartNavigationAdmin", async model => - { - model.ContainedContentTypeDefinitions = GetContainedContentTypes(settings).ToArray(); - model.Container = await GetContainerAsync(containerId); - model.EnableOrdering = settings.EnableOrdering; - model.ContainerContentTypeDefinition = definition; - }).Location("Content:1.5")); + var container = await GetContainerAsync(containerId); - if (settings.ShowHeader) + if (container != null) { - results.Add(GetListPartHeader(containerId, settings)); + // Add list part navigation. + results.Add(Initialize("ListPartNavigationAdmin", model => + { + model.ContainedContentTypeDefinitions = GetContainedContentTypes(settings).ToArray(); + model.Container = container; + model.EnableOrdering = settings.EnableOrdering; + model.ContainerContentTypeDefinition = definition; + }).Location("Content:1.5")); + + if (settings.ShowHeader) + { + results.Add(GetListPartHeader(container, settings)); + } } } } @@ -142,10 +147,10 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten return Combine(results); } - private IDisplayResult GetListPartHeader(string containerId, ListPartSettings listPartSettings) - => Initialize("ListPartHeaderAdmin", async model => + private IDisplayResult GetListPartHeader(ContentItem containerContentItem, ListPartSettings listPartSettings) + => Initialize("ListPartHeaderAdmin", model => { - model.ContainerContentItem = await GetContainerAsync(containerId); + model.ContainerContentItem = containerContentItem; if (listPartSettings != null) { diff --git a/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml b/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml index 91c2fe9f285..4d1e43c6f80 100644 --- a/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.Lists/Views/ListPartHeaderAdmin.cshtml @@ -2,11 +2,6 @@ @inject OrchardCore.ContentManagement.Display.IContentItemDisplayManager ContentItemDisplayManager @inject OrchardCore.DisplayManagement.ModelBinding.IUpdateModelAccessor UpdateModelAccessor -@if (Model.ContainerContentItem == null) -{ - return; -} -
@await DisplayAsync(await ContentItemDisplayManager.BuildDisplayAsync(Model.ContainerContentItem, UpdateModelAccessor.ModelUpdater, "HeaderAdmin"))