Skip to content

Commit

Permalink
Fix an exception in ListPart with header (#14473)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Oct 11, 2023
1 parent cc8efea commit 1c2712f
Showing 1 changed file with 31 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand Down Expand Up @@ -41,7 +40,7 @@ public override async Task<IDisplayResult> 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();
Expand All @@ -66,7 +65,7 @@ await model.AlterAsync<ContainedPart>(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;
Expand Down Expand Up @@ -98,7 +97,7 @@ await model.AlterAsync<ContainedPart>(async part =>
return await EditAsync(model, updater);
}

private IDisplayResult BuildViewModel(string containerId, string containerContentType, string contentType, bool enableOrdering = false)
private async Task<IDisplayResult> BuildViewModelAsync(string containerId, string containerContentType, string contentType, bool enableOrdering = false)
{
var results = new List<IDisplayResult>()
{
Expand All @@ -123,18 +122,23 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten

if (settings != null)
{
// Add list part navigation
results.Add(Initialize<ListPartNavigationAdminViewModel>("ListPartNavigationAdmin", async model =>
{
model.ContainedContentTypeDefinitions = GetContainedContentTypes(settings).ToArray();
model.Container = await _contentManager.GetAsync(containerId, VersionOptions.Latest);
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<ListPartNavigationAdminViewModel>("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));
}
}
}
}
Expand All @@ -143,31 +147,27 @@ private IDisplayResult BuildViewModel(string containerId, string containerConten
return Combine(results);
}

private IDisplayResult GetListPartHeader(string containerId, ListPartSettings listPartSettings)
{
return Initialize<ListPartHeaderAdminViewModel>("ListPartHeaderAdmin", async model =>
private IDisplayResult GetListPartHeader(ContentItem containerContentItem, ListPartSettings listPartSettings)
=> Initialize<ListPartHeaderAdminViewModel>("ListPartHeaderAdmin", model =>
{
var container = await _contentManager.GetAsync(containerId);

if (container == null)
{
return;
}

model.ContainerContentItem = container;
model.ContainerContentItem = containerContentItem;

if (listPartSettings != null)
{
model.ContainedContentTypeDefinitions = GetContainedContentTypes(listPartSettings).ToArray();
model.EnableOrdering = listPartSettings.EnableOrdering;
}
}).Location("Content:1");
}

private IEnumerable<ContentTypeDefinition> GetContainedContentTypes(ListPartSettings settings) =>
settings.ContainedContentTypes
?.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType))
.Where(definition => definition is not null)
?? Enumerable.Empty<ContentTypeDefinition>();
// Initially, attempt to locate a published container.
// If none is found, try acquiring the most recent unpublished version.
private async Task<ContentItem> GetContainerAsync(string containerId)
=> await _contentManager.GetAsync(containerId) ?? await _contentManager.GetAsync(containerId, VersionOptions.Latest);

private IEnumerable<ContentTypeDefinition> GetContainedContentTypes(ListPartSettings settings)
=> settings.ContainedContentTypes
?.Select(contentType => _contentDefinitionManager.GetTypeDefinition(contentType))
.Where(definition => definition is not null)
?? Enumerable.Empty<ContentTypeDefinition>();
}
}

0 comments on commit 1c2712f

Please sign in to comment.